解决:阻塞模式下(blocking)WSARecv失败,error为WSA_IO_PENDING(997)、WSAEOPNOTSUPP (10045)

 

iMode设置为0,为阻塞模式;iMode为非0,为非宿在模式

//-------------------------
  // Set the socket I/O mode: In this case FIONBIO
  // enables or disables the blocking mode for the 
  // socket based on the numerical value of iMode.
  // If iMode = 0, blocking is enabled; 
  // If iMode != 0, non-blocking mode is enabled.
  u_long iMode = 0;
  ioctlsocket(m_socket, FIONBIO, &iMode);

 

1,非阻塞模式下,需要CreateEvent,如果为阻塞模式,则不用Create,要将倒数第二个参数lpOverlapped设为NULL,否则会出现WSA_IO_PENDING(997)错误。

 

2,无论是阻塞模式,还是非阻塞模式,需要将Flags设为0,否则会出现WSAEOPNOTSUPP (10045)错误。由于Flags是in_out类型,所以每次WSARecv前都要设为0。

 

WSAOVERLAPPED RecvOverlapped = {0};
RecvOverlapped.hEvent = WSACreateEvent();
if (RecvOverlapped.hEvent  == NULL) 
{
	fprintf(stderr, "WSACreateEvent failed: %d\n", WSAGetLastError());
	closesocket(ConnSocket);
	return;
}

WSABUF DataBuf;
DWORD RecvBytes, Flags = 0;

char buffer[4096];
DataBuf.len = 4096;
DataBuf.buf = buffer;
	  
int rc = WSARecv(ConnSocket, &DataBuf, 1, &RecvBytes, &Flags, &RecvOverlapped, NULL);

 

你可能感兴趣的:(error)