本文整理汇总了C++中socket_accept函数的典型用法代码示例。如果您正苦于以下问题:C++ socket_accept函数的具体用法?C++ socket_accept怎么用?C++ socket_accept使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_accept函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: netserver_tick
void netserver_tick(NetServer *s)
{
int i;
int n;
if(!s)
return;
if(s->listen_sock != INVALID_SOCKET)
{
SOCKET as;
struct sockaddr client_address = {0};
while((as = socket_accept(&s->listen_sock, &client_address)) != INVALID_SOCKET)
{
NetLink *l = NetLink_Create(s->ctxt);
SOCK_CLEAR_DELAY(as);
sock_set_noblocking(as);
l->sock = as;
l->address = client_address;
l->s = s;
*alnk_push(&s->clients) = l;
}
}
n = alnk_size(&s->clients);
for( i = n - 1; i >= 0; --i )
{
NetLink *c = s->clients[i];
if(!netlink_tick(c))
{
NetLink_Destroy(&c);
alnk_rm(&s->clients,i,1);
}
}
}
示例2: sizeof
LPDESC DESC_MANAGER::AcceptP2PDesc(LPFDWATCH fdw, socket_t bind_fd)
{
socket_t fd;
struct sockaddr_in peer;
char host[MAX_HOST_LENGTH + 1];
if ((fd = socket_accept(bind_fd, &peer)) == -1)
return NULL;
strlcpy(host, inet_ntoa(peer.sin_addr), sizeof(host));
LPDESC_P2P pkDesc = M2_NEW DESC_P2P;
if (!pkDesc->Setup(fdw, fd, host, peer.sin_port))
{
sys_err("DESC_MANAGER::AcceptP2PDesc : Setup failed");
socket_close(fd);
M2_DELETE(pkDesc);
return NULL;
}
m_set_pkDesc.insert(pkDesc);
++m_iSocketsConnected;
sys_log(0, "DESC_MANAGER::AcceptP2PDesc %s:%u", host, peer.sin_port);
P2P_MANAGER::instance().RegisterAcceptor(pkDesc);
return (pkDesc);
}
示例3: socket_server
void socket_server( int server )
{
pthread_t tid;
pthread_attr_t attr;
socket_client_t *client;
// Setup client serving threads to be detached
pthread_attr_init( &attr );
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
while( 1 )
{
// Allocate new space for the client information
client = (socket_client_t*)malloc( sizeof(socket_client_t) );
if( client == NULL )
{
perror( "could not allocate space for client" );
exit( 1 );
}
// Get a new client connection
client->sock = socket_accept( server, &client->addr );
// Spawn a worker to serve the client
pthread_create( &tid, &attr, socket_client, (void*)client );
}
}
示例4: start_multiprocess_server
void start_multiprocess_server(socketfd_t listen_socket, OneConnectionService service_function){
/* wait at receive SIGCHLD, release child resource for multiprocess && concurrent server */
signal(SIGCHLD, sigchid_waitfor_child);
while(1){
socketfd_t client_socket;
SocketAddr client_addr;
client_socket = socket_accept(listen_socket, client_addr);
if( client_socket < 0 ){
perror("accept error");
continue;
}
int child_pid = fork();
if( child_pid == 0 ){
int ret = close(listen_socket);
if( ret < 0 ) perror("close listen_socket error");
service_function(client_socket, client_addr);
ret = close(client_socket);
if( ret < 0 ) perror("close client_socket error");
exit(EXIT_SUCCESS);
}
else if( child_pid > 0 ){
close(client_socket);
}
else {
perror("fork error");
}
}
}
示例5: test_server
static int test_server(struct harness_t *harness_p)
{
struct ssl_context_t context;
struct ssl_socket_t ssl_socket;
struct socket_t listener, socket;
struct inet_addr_t addr;
char buf[8];
/* Create a context with default settings. */
BTASSERT(ssl_context_init(&context) == 0);
/* Create a socket and connect to the server. */
BTASSERT(socket_open_tcp(&listener) == 0);
BTASSERT(socket_listen(&listener, 5) == 0);
BTASSERT(socket_accept(&listener, &socket, &addr) == 0);
/* Wrap the socket in SSL. */
BTASSERT(ssl_socket_init(&ssl_socket,
&context,
&socket,
ssl_socket_mode_server_t) == 0);
BTASSERT(ssl_socket_handshake(&ssl_socket) == 0);
/* Transfer data to and from the server. */
BTASSERT(ssl_socket_read(&ssl_socket, &buf[0], 6) == 6);
BTASSERT(strcmp("hello", buf) == 0);
BTASSERT(ssl_socket_write(&ssl_socket, "goodbye", 8) == 8);
/* Close the connection. */
BTASSERT(socket_close(&socket) == 0);
return (0);
}
示例6: knet_channel_ref_update_accept
void knet_channel_ref_update_accept(kchannel_ref_t* channel_ref) {
kchannel_ref_t* client_ref = 0;
kloop_t* loop = 0;
socket_t client_fd = 0;
verify(channel_ref);
/* 查看选取器是否有自定义实现 */
client_fd = knet_impl_channel_accept(channel_ref);
if (!client_fd) {
/* 默认实现 */
client_fd = socket_accept(knet_channel_get_socket_fd(channel_ref->ref_info->channel));
}
verify(client_fd > 0);
knet_channel_ref_set_state(channel_ref, channel_state_accept);
knet_channel_ref_set_event(channel_ref, channel_event_recv);
if (client_fd) {
loop = knet_channel_ref_choose_loop(channel_ref);
if (loop) {
client_ref = knet_channel_ref_accept_from_socket_fd(channel_ref, loop, client_fd, 0);
verify(client_ref);
knet_channel_ref_set_user_data(client_ref, channel_ref->ref_info->user_data);
/* 设置回调 */
knet_channel_ref_set_cb(client_ref, channel_ref->ref_info->cb);
/* 添加到其他loop */
knet_loop_notify_accept(loop, client_ref);
} else {
client_ref = knet_channel_ref_accept_from_socket_fd(channel_ref, channel_ref->ref_info->loop, client_fd, 1);
verify(client_ref);
knet_channel_ref_set_user_data(client_ref, channel_ref->ref_info->user_data);
/* 调用回调 */
if (channel_ref->ref_info->cb) {
channel_ref->ref_info->cb(client_ref, channel_cb_event_accept);
}
}
}
}
示例7: main
int main(int arg, char *args[])
{
sql_connect(&mysql, &conn);
//如果没有参数,main函数返回
if(arg < 2)
{
printf("usage:myserver port\n");
return EXIT_FAILURE;
}
//setdaemon();
//将第一个参数转化为整数
int iport = atoi(args[1]);
if(iport == 0)
{
printf("port %d is invalid\n", iport);
return EXIT_FAILURE;
}
int st = socket_create(iport);
socket_accept(st);
sql_disconnect(conn);
close(st);
printf("myhttp is end\n");
return 0;
}
示例8: socket_server_serve
bool
socket_server_serve (socket_t *server, socket_t *client, socket_client_handler_t handler)
{
char *buffer;
bool handled;
if (!socket_accept (server, client))
{
return true;
}
buffer = (char *) malloc (sizeof (char) * 1024);
if (!socket_receive (client, buffer, 1024))
{
(void) socket_close (client);
free (buffer);
return true;
}
if (!socket_close (client))
{
free (buffer);
return true;
}
handled = handler ? handler (buffer) : socket_default_client_handler (buffer);
free (buffer);
return handled;
}
示例9: init_socket_example
void init_socket_example(int port, uint8_t *ip)
{
int i = 0;
int socket = socket_open(TCP_STREAM);
struct sock_addr addr;
addr.port = port;
addr.ip = 0;
for(i=0; i<4; i++) {
addr.ip |= ip[i] << i*8;
}
char buffer[11];
// printf("ip is %x\n", addr.ip);
socket_bind(socket, &addr);
socket_listen(socket, 5);
struct sock_addr client;
// printf("Waiting for accept\n");
logger(SOCKET, NORMAL, "waiting on accept\n");
int new_socket = socket_accept(socket, &client);
logger(SOCKET, NORMAL, "coming off accept\n");
socket_send(new_socket, "Hello World", 12);
printf("waiting on socket read\n");
socket_read(new_socket, buffer, 10);
printf("received from socket %s\n", buffer);
socket_close(new_socket);
// printf("accepted the connection\n");
}
示例10: main
int main(void) {
socket_t listener;
socket_set_t clients;
if(false == socket_create_socket(&listener, LIBNET_PROTOCOL_TCP, LIBNET_IPV4)) {
printf("server: failed to create socket\r\n");
return -1;
}
socket_create_set(&clients);
if(false == socket_listen(&listener, PORT)) {
printf("server: failed to listen at %d\r\n", PORT);
return -1;
}
printf("server: listening at %d..\n", PORT);
while(socket_set_get_client_amount(&clients) < 1) {
socket_accept(&listener, &clients);
}
printf("server: client connected (ip: %s)\r\n",
"127.0.0.1");
_sleep(2); // to make sure the client recognizes everything
socket_release_set(&clients);
socket_disconnect(&listener);
socket_release_socket(&listener);
return 0;
}
示例11: init_socket_example
void init_socket_example(int port, uint8_t *ip)
{
int i = 0;
int socket = socket_open(TCP_STREAM);
struct sock_addr addr;
addr.port = port;
addr.ip = 0;
for(i=0; i<4; i++) {
addr.ip |= ip[i] << i*8;
}
// printf("ip is %x\n", addr.ip);
#if 1
socket_bind(socket, &addr);
socket_listen(socket, 5);
struct sock_addr client;
// printf("Waiting for accept\n");
logger(LOG_SOCKET, NORMAL, "waiting on accept\n");
pthread_t thread_id = 0;
while(1) {
int *socket_child = malloc(sizeof(int));
*socket_child = socket_accept(socket, &client);
pthread_create(&thread_id, NULL, DoWork, socket_child);
}
#endif
// printf("accepted the connection\n");
}
示例12: main
int main(int argc,char* argv[])
{
signal(17,signal_handle);
int sfd_listen=socket_init();
int sfd_client;
struct sockaddr_in client_addr;
char buf_client_addr[16]="";
int client_port;
while(1)
{
memset(&client_addr,0,sizeof(struct sockaddr));
sfd_client=socket_accept(sfd_listen,(struct sockaddr*)&client_addr);
if(fork()==0)
{
close(sfd_listen);
strcpy(buf_client_addr,inet_ntoa(client_addr.sin_addr));
client_port=ntohs(client_addr.sin_port);
printf("client %s:%d connect!\n",buf_client_addr,client_port);
child_main(sfd_client);
printf("client %s:%d disconnect!\n",buf_client_addr,client_port);
close(sfd_client);
exit(0);
}
close(sfd_client);
}
return 0;
}
示例13: accept_ready
static void accept_ready(socket_t *socket, UNUSED_ATTR void *context) {
assert(socket != NULL);
assert(socket == listen_socket);
socket = socket_accept(socket);
if (!socket)
return;
client_t *client = (client_t *)osi_calloc(sizeof(client_t));
if (!client) {
LOG_ERROR(LOG_TAG, "%s unable to allocate memory for client.", __func__);
socket_free(socket);
return;
}
client->socket = socket;
if (!list_append(clients, client)) {
LOG_ERROR(LOG_TAG, "%s unable to add client to list.", __func__);
client_free(client);
return;
}
socket_register(socket, thread_get_reactor(thread), client, read_ready, NULL);
}
示例14: socket_init
void My_epoll::handle_query()
{
struct epoll_event events[EPOLLSIZE];
int epollfd=epoll_create(EPOLLSIZE);
if(epollfd==-1)
{
m_log.error("epoll create failed");
}
socket_init();
set_nonblock(server_sock);
QUERY_HANDLE::Query_handle handle(m_conf,&m_log);
epoll_add(epollfd,server_sock,EPOLLIN|EPOLLET);
std::cout<<"server wait for connecting......."<<std::endl;
m_log.notice("server wait for connecting.........");
while(1)
{
int ret=epoll_wait(epollfd,events,EPOLLSIZE,-1);
for(int index=0;index<ret;index++)
{
if(events[index].data.fd==server_sock)
{
int fd_client=socket_accept();
set_nonblock(fd_client);
handle(fd_client);
}
}
}
}
示例15: Accept
bool CPeerBase::Accept(socket_t fd_accept)
{
struct sockaddr_in peer;
if ((m_fd = socket_accept(fd_accept, &peer)) == INVALID_SOCKET)
{
Destroy();
return false;
}
//socket_block(m_fd);
socket_sndbuf(m_fd, 233016);
socket_rcvbuf(m_fd, 233016);
strlcpymt(m_host, inet_ntoa(peer.sin_addr), sizeof(m_host));
m_outBuffer = buffer_new(DEFAULT_PACKET_BUFFER_SIZE);
m_inBuffer = buffer_new(MAX_INPUT_LEN);
if (!m_outBuffer || !m_inBuffer)
{
Destroy();
return false;
}
fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_READ, false);
OnAccept();
sys_log(0, "ACCEPT FROM %s", inet_ntoa(peer.sin_addr));
return true;
}