int nTimeout,=1000;
set
//设置发送超时为1000ms
if( SOCKET_ERROR == setsockopt( sockClient, SOL_SOCKET, SO_SNDTIMEO,
(char *)&nTimeout, sizeof( int ) ) )
{
fprintf( stderr, "Set SO_SNDTIMEO error !\n" );
}
//设置接收超时为1000ms
if( SOCKET_ERROR == setsockopt( sockClient, SOL_SOCKET, SO_RCVTIMEO,
(char *)&nTimeout, sizeof( int ) ) )
{
fprintf( stderr, "Set SO_RCVTIMEO error !\n" );
}
注意:
1、rev() 的第四个参数 需要设为 MSG_WAITALL,一直阻塞,知道指定数目的数据到来才返回,除非超时时间到达。如果,设置了接收超时,在没有MSG_WAITALL,情况下,也是有效的,
2.即使等待超时时间值未到,对方关闭了socket()。rev()会立即返回接收到的数据字节。
#include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") int main(int argc,char **argv) { //创建套接字 WORD myVersionRequest; WSADATA wsaData; myVersionRequest=MAKEWORD(1,1); int err; err=WSAStartup(myVersionRequest,&wsaData); if (!err){ printf("已打开套接字\n"); }else{ printf("ERROR:嵌套字未打开!"); return 1; } //进一步绑定套接字 SOCKET serSocket=socket(AF_INET,SOCK_STREAM,0);//创建了可识别套接字 //需要绑定的参数 SOCKADDR_IN addr; addr.sin_family=AF_INET; addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//ip地址 addr.sin_port=htons(6000);//绑定端口 bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));//绑定完成 listen(serSocket,5);//其中第二个参数代表能够接收的最多的连接数 // //开始进行监听 // SOCKADDR_IN clientsocket; int len=sizeof(SOCKADDR); while (1) { SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);//如果这里不是accept而是conection的话。。就会不断的监听 char sendBuf[100]; sprintf(sendBuf,"hello, %s !",inet_ntoa(clientsocket.sin_addr));//找对对应的IP并且将这行字打印到那里 printf("Send:%s\n",sendBuf); send(serConn,sendBuf,strlen(sendBuf)+1,0); char receiveBuf[100];//接收 recv(serConn,receiveBuf,sizeof(receiveBuf),0); printf("recv:%s\n",receiveBuf); closesocket(serConn);//关闭 WSACleanup();//释放资源的操作 return 0; } return 1; }
Client
bool CTcpSocket::Connect()
{
//SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
sockClient = socket( AF_INET, SOCK_STREAM, 0 );
SOCKADDR_IN addrSrv;
//addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_addr.S_un.S_addr = inet_addr(m_server.c_str());
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons( m_port );
int nTimeout = TCP_TIME_MAX;
//设置发送超时为1000ms
if( SOCKET_ERROR == setsockopt( sockClient, SOL_SOCKET, SO_SNDTIMEO,
(char *)&nTimeout, sizeof( int ) ) )
{
fprintf( stderr, "Set SO_SNDTIMEO error !\n" );
}
//设置接收超时为1000ms
if( SOCKET_ERROR == setsockopt( sockClient, SOL_SOCKET, SO_RCVTIMEO,
(char *)&nTimeout, sizeof( int ) ) )
{
fprintf( stderr, "Set SO_RCVTIMEO error !\n" );
}
if( connect( sockClient, (SOCKADDR*)&addrSrv, sizeof( SOCKADDR ) ) == 0 )
{
// CLOG::Out0( L"DEBUG", L"%d.client connect:%s,%d" ,sockClient,CComon::ToCString( inet_ntoa(addrSrv.sin_addr)),ntohs(addrSrv.sin_port));
return true;
} else
{
return false;
}
//true;
}
接收开辟一个线程
/**
* @brief Receive datas from severs.
* @note
* @param
* @retval None
*/
int CTcpSocket::Receive( __out_bcount_part(len, return ) __out_data_source(NETWORK) char FAR * buf,
__in int len )
{
int re = recv( sockClient, buf, len, 0 );
return re;
}
/**
* @brief Receive datas from severs.
* @note
* @param
* @retval None
*/
UINT SocketReceivePross(void *pagram)
{
CTcpSocket *temp= (CTcpSocket*) pagram;
while (1)
{
memset(temp->dataBuf,0,BUF_SZIE);
int length= temp->Receive(temp->dataBuf, sizeof(temp->dataBuf)); //阻塞等待5S
if ( length>0)
{
temp->Analysis();
}
::Sleep(1);
}
}