当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

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

发表于: 2013-08-01   作者:aigo   来源:转载   浏览:
摘要:   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

 

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);

 

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号