Winsock例子代码:初始化、阻塞/非阻塞、超时设置、select等

WSADATA wsd; SOCKET cClient; int ret; struct sockaddr_in server; hostent *host=NULL; if(WSAStartup(MAKEWORD(2,0),&wsd)){return 0;} cClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(cClient==INVALID_SOCKET){return 0;} //set Recv and Send time out int TimeOut=6000; //设置发送超时6秒 if(::setsockopt(cClient,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR){ return 0; } TimeOut=6000;//设置接收超时6秒 if(::setsockopt(cClient,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR){ return 0; } //设置非阻塞方式连接 unsigned long ul = 1; ret = ioctlsocket(cClient, FIONBIO, (unsigned long*)&ul); if(ret==SOCKET_ERROR)return 0; //连接 server.sin_family = AF_INET; server.sin_port = htons(25); server.sin_addr .s_addr = inet_addr((LPCSTR)pSmtp); if(server.sin_addr.s_addr == INADDR_NONE){return 0;} connect(cClient,(const struct sockaddr *)&server,sizeof(server)); //select 模型,即设置超时 struct timeval timeout ; fd_set r; FD_ZERO(&r); FD_SET(cClient, &r); timeout.tv_sec = 15; //连接超时15秒 timeout.tv_usec =0; ret = select(0, 0, &r, 0, &timeout); if ( ret <= 0 ) { ::closesocket(cClient); return 0; } //一般非锁定模式套接比较难控制,可以根据实际情况考虑 再设回阻塞模式 unsigned long ul1= 0 ; ret = ioctlsocket(cClient, FIONBIO, (unsigned long*)&ul1); if(ret==SOCKET_ERROR){ ::closesocket (cClient); return 0; }

 

// 等待连接,nListenSock是非阻塞的,返回的也为非阻塞的 static int waitConnect( int nListenSock, int nTimeout ) { struct timeval time0, time1; fd_set rdfds; int nSel; FD_ZERO(&rdfds); FD_SET(nListenSock, &rdfds); if( nTimeout > 0 ) { struct timeval tv; tv.tv_sec = 0; tv.tv_usec = nTimeout*1000; gettime(&time0); while( (nSel = select(nListenSock+1,0, &rdfds, 0, &tv)) == -1 && errno == EINTR ) { gettime(&time1); nTimeout -= TIMEDELTA_MS(time1,time0); if(nTimeout<=0) { nSel = 0; //按照超时处理 break; } time0 = time1; tv.tv_usec = nTimeout*1000; } } else //while( (nSel = select(nListenSock+1, &rdfds, 0, 0, 0)) == -1 && errno == EINTR ); while( (nSel = select(nListenSock+1,0, &rdfds, 0, 0)) == -1 && errno == EINTR ); return nSel; } int socket_connect( const char* pServerAddress, int nServerPort ,int timeout) { char* ip; int nRet = -1; int nReuse = 1; struct sockaddr_in serverAddr; int ret; struct hostent* pHost =NULL; int nSock = (int)socket(AF_INET, SOCK_STREAM, 0); if( nSock == -1 ) return -1; memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; if( !pServerAddress || !*pServerAddress ) // 未指定地址 goto fail; //if( !inet_aton(pServerAddress, &(serverAddr.sin_addr)) ) // 无效点地址 ret =inet_addr(pServerAddress); if( ret==INADDR_NONE||ret==0 ) { pHost= gethostbyname(pServerAddress); if( !pHost ) // 无效的地址 goto fail; ip = inet_ntoa(*(struct in_addr *)*pHost->h_addr_list); // if( !inet_aton(ip, &(serverAddr.sin_addr)) ) // 无效点地址 ret =inet_addr(ip); if( ret==INADDR_NONE||ret==0 ) goto fail; } serverAddr.sin_addr = *((struct in_addr*)&ret); serverAddr.sin_port = htons(nServerPort); setNonBlocking( nSock, TRUE ); connect(nSock, (struct sockaddr*)&serverAddr, sizeof(serverAddr) ); nRet = waitConnect(nSock, timeout); setNonBlocking( nSock, FALSE ); if(nRet <= 0) goto fail; return nSock; fail: socket_close(nSock); return nRet; }

你可能感兴趣的:(socket,Stream,list,tcp,struct,server)