**************同步阻塞socket代码***********************
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet; char dest[1024];
if(WSAStartup(winsock_version,&wsadata))
printf("can't open"); lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
saServer.sin_family = AF_INET;
saServer.sin_port = htons(1234);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
{
printf("connected with %s\n",host_name);
} nRet = send(hsocket, hostname, strlen(hostname), 0);
if(!nRet)
{
AfxMessageBox(L"发送请求失败!");
closesocket(hsocket);
return ;
}
nRet = recv(hsocket,(LPSTR)dest,sizeof(dest),0);
closesocket(hsocket);
*****************************************************************请问如果要用select做超时处理,以上代码应该怎么改?
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet; char dest[1024];
if(WSAStartup(winsock_version,&wsadata))
printf("can't open"); lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
saServer.sin_family = AF_INET;
saServer.sin_port = htons(1234);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
{
printf("connected with %s\n",host_name);
} nRet = send(hsocket, hostname, strlen(hostname), 0);
if(!nRet)
{
AfxMessageBox(L"发送请求失败!");
closesocket(hsocket);
return ;
}
nRet = recv(hsocket,(LPSTR)dest,sizeof(dest),0);
closesocket(hsocket);
*****************************************************************请问如果要用select做超时处理,以上代码应该怎么改?
简单的socket+select使用
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet; char dest[1024];
if(WSAStartup(winsock_version,&wsadata))
printf("can't open"); lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
saServer.sin_family = AF_INET;
saServer.sin_port = htons(1234);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
{
printf("connected with %s\n",host_name);
}fd_set fdRead, fdWrite;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
FD_SET(hsocket, &fdRead);
FD_SET(hsocket, &fdWrite);
struct timeval tm;
tm.tv_sec = 100; // 设置100秒为超时时间while(true)
{
if(nRet = select(0, &fdRead, &fdWrite, NULL, &tm) == SOCKET_ERROR)
{
// 出错处理
}
if(nRet > 0)
{
if(FD_ISSET(hsocket, fdWrite))
{
nRet = send(hsocket, hostname, strlen(hostname), 0);
if(!nRet)
{
AfxMessageBox(L"发送请求失败!");
closesocket(hsocket);
return ;
}
}
if(FD_ISSET(hsocket, fdRead))
{
nRet = recv(hsocket,(LPSTR)dest,sizeof(dest),0);
closesocket(hsocket);
return;
}
}
}