#include <stdio.h>
#include <winsock2.h>
#define TCP
#pragma comment(lib,"ws2_32.lib")
int main( int argc ,char* argv[])
{
WSAData wsaData;
WSAStartup(WINSOCK_VERSION,&wsaData);
DWORD begin,finish;
struct timeval tv;
tv.tv_sec = 5000;
tv.tv_usec = 0;
int optlen = sizeof(struct timeval);
//
SOCKET sock = socket(PF_INET,SOCK_STREAM,0);
struct sockaddr_in to;
int len = sizeof(struct sockaddr_in);
memset(&to,0,len);
to.sin_addr.s_addr = inet_addr("202.108.9.39");;
to.sin_port = htons(80);
to.sin_family = AF_INET;
if ( connect(sock,(struct sockaddr*)&to,len) == SOCKET_ERROR )
{
closesocket(sock);
return 0;
}
//设置超时值为tv
if ( setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&tv,optlen) ==
SOCKET_ERROR)
{
closesocket(sock);
return 0;
}
char buf[100];
if ( recv(sock,buf,100,0) == SOCKET_ERROR )
{
printf("d");
}
return 0;
}