呵呵,理解得不深,但毕竟手打全版,且无错。但select.h不知何处找头文件,
粘下来作个记录。
POLL,EPOLL感觉代码类似,只是函数和系统实现不一样,,EPOLL目前最合理的。定位精确,算法复杂度最好。
#include "select.h"#include#include #include #include #include #include #include #include typedef struct _CLIENT{ int fd; struct sockaddr_in addr;} CLIENT;#define MYPORT 59000#define BACKLOG 5CLIENT client[BACKLOG];int currentClient = 0;#define REVLEN 10char recvBuf[REVLEN];void showClient();int main(){ int i, ret, sinSize; int recvLen = 0; fd_set readfds, writefds; int sockListen, sockSvr, sockMax; struct timeval timeout; struct sockaddr_in server_addr; struct sockaddr_in client_addr; for(i=0; i 0) { FD_SET(client[i].fd, &readfds); if(sockMax 0 && FD_ISSET(client[i].fd, &readfds)) { if(recvLen != REVLEN) { while(1) { ret = recv(client[i].fd, (char *)recvBuf+recvLen, REVLEN-recvLen, 0); if(ret == 0) { client[i].fd = -1; recvLen = 0; break; } else if(ret < 0) { client[i].fd = -1; recvLen = 0; break; } recvLen = recvLen + ret; if(recvLen < REVLEN) { continue; } else { printf("%s, buf = %s\n", inet_nota(client[i].addr.sin_addr), recvBuf); recvLen = 0; break; } } } } } if(FD_ISSET(sockListen, &readfds)) { printf("isset\n"); sockSvr = accept(sockListen, NULL, NULL); if(sockSvr == -1) { printf("accept error\n"); } else { currentClient++; } for(i=0; i< BACKLOG; i++) { if(client[i].fd < 0) { client[i].fd = sockSvr; client[i].addr = client_addr; printf("You got a connection from %s \n", inet_ntoa(client[i].addr.sin_addr)); break; } } } } printf("test\n"); return 0;}void showClient(){ int i; printf("client count = %d\n", currentClient); for(i=0; i