|
发表于 2020-8-15 21:15:02
|
显示全部楼层
不好意思,前面不是Receive,应该是.recv();听说是阻塞模式造成的,该怎么解决呢
解决方法:
把它设置超时.
以下仅供参考:
int **::ConnectService( )//连接函数
{
sockClient=socket(AF_INET,SOCK_STREAM,0 );
//unsigned long ul = 1;
//int nRet = ioctlsocket(sockClient, FIONBIO, (unsigned long*)&ul);
int TimeOut=60000; //设置发送超时6秒
if(::setsockopt(sockClient,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR){
return 1;
}
TimeOut=60000;//设置接收超时6秒
if(::setsockopt(sockClient,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR){
return 1;
}
addrSrv.sin_addr.S_un.S_addr=inet_addr( m_serverIP );
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons( m_serverPort );
connect_return=connect(sockClient,(SOCKADDR*)&addrSrv,sizeof( SOCKADDR ));
return connect_return;
} |
|