有关UDP非阻塞recvfrom超时设置的问题
本帖最后由 lingducool 于 2013-05-21 21:04:20 编辑 我现在想编这么一个程序,向目标端口发送UDP的一个包后,用recvfrom等待接受回应,等待5秒后未接到回应就继续向下执行。
我的思路是设置为非阻塞式套接字,然后设置超时5秒,但是这样我没有成功,代码如下:
#include <winsock2.h>
#include <iostream.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
void initClient();
int main()
{
initClient();
return 0;
}
void initClient()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(2,2),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSend;
sockSend.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockSend.sin_port=htons(4000);
sockSend.sin_family=AF_INET;
char buff[1024];
strcpy(buff,"hello,it's the first!");
int iMode = 1;//block
ioctlsocket(s, FIONBIO, (u_long FAR*) &iMode);
DWORD TimeOut=1000*5; //设置发送超时10秒
if(::setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR)
{
cout<<"设置失败"<<endl;
}
int lenword;
lenword=sendto(s,buff,strlen(buff)+1,0,(sockaddr *)&sockSend,sizeof(sockaddr));
cout<<lenword<<","<<sockSend.sin_port<<":"<<sockSend.sin_addr.S_un.S_addr<<endl;
char recBuff[1024];
memset(recBuff,0,1024);
SOCKADDR_IN sockRec;
int len=sizeof(SOCKADDR);
recvfrom(s,recBuff,sizeof(recBuff),0,(sockaddr *)&sockRec,&len);
printf("the receive is\n");
closesocket(s);
WSACleanup();
}
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void initClient();
int main()
{
initClient();
return 0;
}
void initClient()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(2,2),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=2
[解决办法]
HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSend;
sockSend.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockSend.sin_port=htons(6788);
sockSend.sin_family=AF_INET;
char * buff = "hello,it's the first!";
int lenword;
SOCKADDR_IN sockme;
char recBuff[1024];
DWORD TimeOut=1000*5; //设置发送超时10秒
memset(recBuff,0,1024);
sockme.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockme.sin_port=htons(9686);
sockme.sin_family=AF_INET;
#if 1
lenword = bind(s, (sockaddr *)&sockme,sizeof(sockme));
if (lenword != 0) {
printf("bind failed with error %d\n", WSAGetLastError());
return;
}
#endif
if(::setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR)
{
cout<<"设置失败"<<endl;
}
//lenword=sendto(s,buff,strlen(buff),0,(sockaddr *)&sockSend,sizeof(sockaddr));
//cout<<lenword<<","<<sockSend.sin_port<<":"<<sockSend.sin_addr.S_un.S_addr<<endl;
lenword = recvfrom(s,recBuff,sizeof(recBuff),0, NULL, NULL);
printf("recvfrom data %d:%s\n", lenword, recBuff);
if (lenword < 0) {
printf("recvfrom failed with error %d\n", WSAGetLastError());
return;
}
closesocket(s);
WSACleanup();
}
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void initClient();
int main()
{
initClient();
return 0;
}
void initClient()
{
WSADATA wsaData;
int error=WSAStartup(MAKEWORD(2,2),&wsaData);
if(error!=0)
{
cout<<"初始化DLL失败"<<endl;
return;
}
if(LOBYTE(wsaData.wVersion)!=2
[解决办法]
HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
cout<<"版本出错"<<endl;
return;
}
SOCKET s=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN sockSend;
sockSend.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockSend.sin_port=htons(6788);
sockSend.sin_family=AF_INET;
char * buff = "hello,it's the first!";
int lenword;
SOCKADDR_IN sockme;
char recBuff[1024];
DWORD TimeOut=1000*5; //设置发送超时10秒
memset(recBuff,0,1024);
sockme.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
sockme.sin_port=htons(9686);
sockme.sin_family=AF_INET;
#if 0
lenword = bind(s, (sockaddr *)&sockme,sizeof(sockme));
if (lenword != 0) {
printf("bind failed with error %d\n", WSAGetLastError());
return;
}
#endif
if(::setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR)
{
cout<<"设置失败"<<endl;
}
lenword=sendto(s,buff,strlen(buff),0,(sockaddr *)&sockSend,sizeof(sockaddr));
cout<<lenword<<","<<sockSend.sin_port<<":"<<sockSend.sin_addr.S_un.S_addr<<endl;
lenword = recvfrom(s,recBuff,sizeof(recBuff),0, NULL, NULL);
printf("recvfrom data %d:%s\n", lenword, recBuff);
if (lenword < 0) {
printf("recvfrom failed with error %d\n", WSAGetLastError());
return;
}
closesocket(s);
WSACleanup();
}