如何用select()函数对套接字I/O进行管理?(我看书了,可是不会用,书上没示例)
我要具体用法,请斑竹帮帮忙,下面我给出个简单的服务器代码,
斑竹帮我用SELECT()函数对my_sockfd client_sockfd 进行管理
并给出代码 谢谢了!! /*program for send "congratulations"*/ #include <winsock2> #include <stdio> #include <iostream>
using namespace std;
void main() { int Ret,sin_size,my_port=5150; SOCKET my_sockfd,client_sockfd; WSADATA data; int ret; SOCKADDR_IN my_addr,client_addr; my_addr.sin_family=AF_INET; my_addr.sin_port=htons(my_port); my_addr.sin_addr.s_addr=htonl(INADDR_ANY); sin_size=sizeof(SOCKADDR_IN); if((Ret=WSAStartup(MAKEWORD(2,2),&data))!=0) { printf("WSAStartup failed with error %d \n",Ret); exit(1); } if((my_sockfd=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP))==SOCKET_ERROR) { printf("Got socket failed with error %d \n", WSAGetLastError()); WSACleanup(); exit(1); } if((bind(my_sockfd,(SOCKADDR*)&my_addr, f sizeof(SOCKADDR)))==SOCKET_ERROR) { printf("Bind failed with error %d \n", WSAGetLastError()); closesocket(my_sockfd); WSACleanup(); exit(1); } if((listen(my_sockfd,2))==SOCKET_ERROR) { printf("Listen failed with error %d \n", WSAGetLastError()); closesocket(my_sockfd); WSACleanup(); exit(1); } if((client_sockfd=accept(my_sockfd, (SOCKADDR*)&client_addr,&sin_size)) ==SOCKET_ERROR) { printf("accept failed with error %d \n", WSAGetLastError()); closesocket(my_sockfd); WSACleanup(); exit(1); } printf("Server: got connect from %s \n", inet_ntoa(client_addr.sin_addr)); if((send(client_sockfd,"Congratulations!!", 18,0))==SOCKET_ERROR) { printf("Send failed with error %d \n", WSAGetLastError()); closesocket(client_sockfd); closesocket(my_sockfd); WSACleanup(); exit(1); } printf("send data successfully\n"); closesocket(client_sockfd); closesocket(my_sockfd); WSACleanup(); }