我正在使用ioctlsocket()函数使我的套接字无阻塞但是当我调用recvfrom()时,我得到错误10035(WSAEWOULDBLOCK
)。
u_long mode = 1;
ioctlsocket(newSocketIdentifier, FIONBIO, &mode);
while(1)
{
if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
{
char err[128];
itoa(WSAGetLastError(),err,10);
MessageBox( NULL,"Could not Receive Data",err,MB_ICONINFORMATION);
BREAK;
}
}
有人可以解释为什么会这样吗? :(
答案 0 :(得分:11)
如果没有可用的数据,这是正常的。代码是WSAEWOULDBLOCK(参见此表),意思是,在阻塞端口上,该函数必须等待,直到它可以被提供。
while(1)
{
if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
{
int ierr= WSAGetLastError();
if (ierr==WSAEWOULDBLOCK) { // currently no data available
Sleep(50); // wait and try again
continue;
}
// Other errors
char err[128];
itoa(ierr,err,10);
MessageBox( NULL,"Could not Receive Data",err,MB_ICONINFORMATION);
break;
}
}
答案 1 :(得分:0)
我不同意这是上面所述的“正常”情况。
在您致电recvfrom
时,您会收到recv_len
的错误消息。我建议检查该值-它将是SOCKET_ERROR
,并通过调用WSAGetLastErrorenter
会看到错误WSAEWOULDBLOCK
。
我不是(Windows)套接字专家,但是根据我的测试,我不能使用ioctlsocket
和recvfrom
的组合以非阻塞模式通过UDP接收数据(我做了同样的事情在您的示例中像您这样的事情。
我计划使用select
和recvfrom
的组合,并尽可能减少超时时间(1us)。我现在不知道任何其他可能更好的解决方案。
注意:您还应该检查ioctlsocket
的返回值以了解可能的错误。
我将在今天晚些时候提供我的代码示例。
更新(按承诺添加代码):
/* define list of sockets for function select(..) */
fd_set readfds;
/* define timeout for function select(..) */
TIMEVAL tv;
/* timeout: 1us */
tv.tv_usec = 1;
/* timeout: 0s */
tv.tv_sec = 0;
/* just 1 socket is used */
readfds.fd_count = 1;
readfds.fd_array[0] = receivingSocket;
/* determine the status of one or more sockets with timeout */
int selectReturnValue = select(0, &readfds, 0, 0, &tv);
/* check return value of the call of function select(..) */
switch (selectReturnValue)
{
/* select(..) function timeout */
case 0:
/* time limit expired */
break;
/* select(..) function error */
case SOCKET_ERROR:
/* check the error status for the last windows sockets operation */
selectError(WSAGetLastError());
break;
/* no timeout and no error */
default:
/* receive data from UDP */
resultOfrecvfrom = recvfrom(receivingSocket, receivingBuffer, sizeof(receivingBuffer), 0, (SOCKADDR *)&serverReceptionInfo, &serverReceptionInfoLength);
/* check result of call of recvfrom(..) */
switch (resultOfrecvfrom)
{
/* connection has been gracefully closed */
case 0:
/* socket was closed */
break;
/* socket error occurred during last call of socket operation */
case SOCKET_ERROR:
/* check the error status for the last Windows Sockets operation */
recvfromError(WSAGetLastError());
break;
/* resultOfrecvfrom amount of data received */
default:
/* ... add your code here */
break;
}
break;
}
如果需要更多内容,我只是复制了代码的关键部分,请在注释中告诉我。