简体   繁体   English

螺纹和 select 插座 c

[英]Thread and select socket c

I am very new in network programming and am very confused about system call select.我是网络编程的新手,对系统调用 select 非常困惑。 I am not very sure when I should use select or thread to manage a multiclient server (or both)我不太确定何时应该使用 select 或线程来管理多客户端服务器(或两者)

For example, I have wrote this code to manage an indefinite number of clients.例如,我编写了这段代码来管理不定数量的客户端。 For every client I create a thread, read a message and wait until client send another one.我为每个客户创建一个线程,读取一条消息并等待客户发送另一个消息。 Is this right or I should use a select in this case?这是正确的还是在这种情况下我应该使用 select ?

//Called after listen()
void connectionRequestManagement(){
    struct sockaddr_in client;
    pthread_t tid;

    while(1){ 
       socket_accept = accept(socket_fd,(struct sockaddr* )&client, (socklen_t*)&size_connection);
       PtrClientStruct new_client = new_clientInfo(socket_accept,inet_ntoa(client.sin_addr));
       insert_client(&head_client_list, new_client);
       pthread_create(&tid,NULL,connection_handler, (void*)new_client);
       new_client->tid_handler=tid;
       pthread_detach(tid); 
   }
}

void *connection_handler(void* args){    
   PtrClientStruct tc = (PtrClientStruct)args;
   int sock=*(int*) (tc->socket_thread); 
   char op [2] = {'\n'};
   while(true){
       if(readn(sock,op,2)<0)
            break;  
       if(atoi(op) == 0)
           break;
       chose_op(sock,atoi(op), &tc); //Just some read and write
   }  
   //Delete node and his stuff
   delete_client(&head_client_list,*(((PtrClientStruct)args)->socket_thread));
   close(sock);
   pthread_exit(0);
}

I am very new in network programming and am very confused about system call select.我是网络编程的新手,对系统调用 select 非常困惑。 I am not very sure when I should use select or thread to manage a multiclient server (or both)我不太确定何时应该使用 select 或线程来管理多客户端服务器(或两者)

For example, I have wrote this code to manage an indefinite number of clients.例如,我编写了这段代码来管理不定数量的客户端。 For every client I create a thread, read a message and wait until client send another one.我为每个客户创建一个线程,读取一条消息并等待客户发送另一个消息。 Is this right or I should use a select in this case?这是正确的还是在这种情况下我应该使用 select ?

//Called after listen()
void connectionRequestManagement(){
    struct sockaddr_in client;
    pthread_t tid;

    while(1){ 
       socket_accept = accept(socket_fd,(struct sockaddr* )&client, (socklen_t*)&size_connection);
       PtrClientStruct new_client = new_clientInfo(socket_accept,inet_ntoa(client.sin_addr));
       insert_client(&head_client_list, new_client);
       pthread_create(&tid,NULL,connection_handler, (void*)new_client);
       new_client->tid_handler=tid;
       pthread_detach(tid); 
   }
}

void *connection_handler(void* args){    
   PtrClientStruct tc = (PtrClientStruct)args;
   int sock=*(int*) (tc->socket_thread); 
   char op [2] = {'\n'};
   while(true){
       if(readn(sock,op,2)<0)
            break;  
       if(atoi(op) == 0)
           break;
       chose_op(sock,atoi(op), &tc); //Just some read and write
   }  
   //Delete node and his stuff
   delete_client(&head_client_list,*(((PtrClientStruct)args)->socket_thread));
   close(sock);
   pthread_exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM