执行WSARecvFrom时候出错,求解决!

恨天低 2011-05-17 02:02:50
当我在debug的时候,执行到该函数时,程序停止运行!没报任何错误,请问这是怎么回事?
...全文
165 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanheifeng715000 2011-05-17
  • 打赏
  • 举报
回复
你看看socket在初始化时是怎么定义的,因为你用的是重叠I/O模式,所以要设置overlapped属性。如果你用的是socket就没事,但是如果你用的是WSASocket,在最后一个参数那里要设置为WSA_FLAG_OVERLAPPED,这样,它就会把每一个每一个套接字与一个事件关联在一起,设置为非阻塞模式,就算没有数据来了,WSARecvFrom 都会立刻返回。至于是套接字什么时候收到信息或者要进行处理,用的是WSAWaitForMulipleEvents函数下面的就不说了,自己去看看吧。
lijianli9 2011-05-17
  • 打赏
  • 举报
回复
m_udp_socket是异步socket吗?
ouyh12345 2011-05-17
  • 打赏
  • 举报
回复
If lpCompletionRoutine is not NULL, the hEvent parameter is ignored and can be used by the application to pass context information to the completion routine. A caller that passes a non-NULLlpCompletionRoutine and later calls WSAGetOverlappedResult for the same overlapped I/O request may not set the fWait parameter for that invocation of WSAGetOverlappedResult to TRUE. In this case the usage of the hEvent parameter is undefined, and attempting to wait on the hEvent parameter would produce unpredictable results.

The completion routine follows the same rules as stipulated for Windows file I/O completion routines. The completion routine will not be invoked until the thread is in an alertable wait state such as can occur when the function WSAWaitForMultipleEvents with the fAlertable parameter set to TRUE is invoked.

The transport providers allow an application to invoke send and receive operations from within the context of the socket I/O completion routine, and guarantee that, for a given socket, I/O completion routines will not be nested. This permits time-sensitive data transmissions to occur entirely within a preemptive context.

恨天低 2011-05-17
  • 打赏
  • 举报
回复

int CUdpSocket::PostRequestData(int i)
{
int err=0; // Error code
ULONG ulRevBytes; // Number of bytes received
ULONG flags=0; // Receive flags
// We use completion routing, so event field can be utilized for additional parameter
m_overlap.hEvent = (HANDLE)(&m_overlapBuf[i]);
err = WSARecvFrom (m_udp_socket,
&m_wsaBuf[i],
1,
&ulRevBytes,
&flags,
(PSOCKADDR)&m_fromAddr,
&m_fromAddrLen,
&m_overlap,
RcvCompletion
);
if((err==0) || ((err=WSAGetLastError ())==WSA_IO_PENDING))
{
return 0;
}
return err;
}
ouyh12345 2011-05-17
  • 打赏
  • 举报
回复
代码?
恨天低 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ouyh12345 的回复:]

先确认数据是否已经到了网卡上
[/Quote]
有数据过来的。
ouyh12345 2011-05-17
  • 打赏
  • 举报
回复
先确认数据是否已经到了网卡上
恨天低 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 visualeleven 的回复:]

引用 3 楼 icechenbing 的回复:
有发数据跟没没发数据都一样的效果。

同事说是没设置异步接收??

这是怎么回事?求解释!

你用的是阻塞模式的
[/Quote]

那怎么设置为异步接收呢?
Eleven 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 icechenbing 的回复:]
有发数据跟没没发数据都一样的效果。

同事说是没设置异步接收??

这是怎么回事?求解释!
[/Quote]
你用的是阻塞模式的
恨天低 2011-05-17
  • 打赏
  • 举报
回复
有发数据跟没没发数据都一样的效果。

同事说是没设置异步接收??

这是怎么回事?求解释!
ouyh12345 2011-05-17
  • 打赏
  • 举报
回复
应该是阻塞了,没有向这台机器发数据?
Eleven 2011-05-17
  • 打赏
  • 举报
回复
当我在debug的时候,执行到该函数时,程序停止运行!没报任何错误,请问这是怎么回事?
----------------------------
阻塞了??

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧