聊聊select, poll 和 epoll
聊聊select, poll 和 epoll 假设项目上需要实现一个TCP的客户端和服务器从而进行跨机器的数据收发,我们很可能翻阅一些资料,然后写出如下的代码。 服务端 void func( int sockfd) { char buff[MAX]; int n; // infinite loop for chat for (;;) { bzero(buff, MAX); // read the message from client and copy it in buffer read(sockfd, buff, sizeof (buff)); // print buffer which contains the client contents printf( " From client: %s\t To client : " , buff); bzero(buff, MAX); n = 0 ; // copy server message in the buffer while ((buff[n++] = getchar()) != ' \n ' ) ; // and send that buffer to client write(sockfd, buff, sizeof (buff)); // if msg contains "Exit" then server exit