关于socket的receive

sjjwind 2011-11-16 09:07:54
我在一个线程里加了while(-1 == revc(...))可是会卡内存,如果不加while这个语句没有接受buf就跳过了,显示的buf是乱码,要怎么解决。
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjjwind 2011-11-16
  • 打赏
  • 举报
回复
查了下msdn 的确是要CReate的,我给忘了,谢谢。
leegoex 2011-11-16
  • 打赏
  • 举报
回复
m_ReceiveMessage要调用Create函数,指明用UDP还是TCP,要指定端口。
看看这个吧

http://msdn.microsoft.com/en-us/library/t7a47kk4.aspx
sjjwind 2011-11-16
  • 打赏
  • 举报
回复
是从CSocket继承而来的一个类
leegoex 2011-11-16
  • 打赏
  • 举报
回复
m_ReceiveMessage有创建吗?
sjjwind 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 leegoex 的回复:]
你创建的socket有问题吧,要不然怎么会返回-1.

If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR ……
[/Quote]
CMySocket m_ReceiveMessage;
while(true)
{
TCHAR* pBuffer = new TCHAR[1024];
m_ReceiveMessage.Receive(pBuffer,sizeof(pBuffer));
::SendMessage((HWND)pParam,WM_RECEIVE_MESSAGE,(WPARAM)pBuffer,0);
}
我这样写有问题么?
leegoex 2011-11-16
  • 打赏
  • 举报
回复
你创建的socket有问题吧,要不然怎么会返回-1.

If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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