CAsyncSocket类的Detch函数和Close函数各自的作用是什么?有和区别?

wj_xiazi 2003-08-26 10:43:03
他们和API函数和closesocket有什么联系?
...全文
278 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wj_xiazi 2003-09-15
  • 打赏
  • 举报
回复
我认为这是MFC对象和windows对象之间的关系:
CAsyncSocket对象是一个MFC对象。它里面包含了一个windows对象,也就是套接字的成员变量m_hSocket指向的东西。这个东西要占用系统资源。我可以把它释放掉,然后我再用CAsyncSocket对象的Create函数再申请一个windows对象,并把它和CAsyncSocket对象Attach 到一起。在这个过程中MFC对象并不需要释放掉然后重新建立一个。
farfh 2003-08-27
  • 打赏
  • 举报
回复
Detach本身不closesocket,所以你在这里Detach,没有任何意义,不会释放资源。

关于shutdown的用法,msdn上很清楚,以前没用过,刚看了下^_^(俺也菜鸟),抄录如下:
The shutdown function is used on all types of sockets to disable reception, transmission, or both.

If the how parameter is SD_RECEIVE, subsequent calls to the recv function on the socket will be disallowed. This has no effect on the lower protocol layers. For TCP sockets, if there is still data queued on the socket waiting to be received, or data arrives subsequently, the connection is reset, since the data cannot be delivered to the user. For UDP sockets, incoming datagrams are accepted and queued. In no case will an ICMP error packet be generated.

If the how parameter is SD_SEND, subsequent calls to the send function are disallowed. For TCP sockets, a FIN will be sent after all data is sent and acknowledged by the receiver.

Setting how to SD_BOTH disables both sends and receives as described above.

The shutdown function does not close the socket. Any resources attached to the socket will not be freed until closesocket is invoked.

To assure that all data is sent and received on a connected socket before it is closed, an application should use shutdown to close connection before calling closesocket. For example, to initiate a graceful disconnect:


Call WSAAsyncSelect to register for FD_CLOSE notification.
Call shutdown with how=SD_SEND.
When FD_CLOSE received, call recv until zero returned, or SOCKET_ERROR.
Call closesocket.

Note The shutdown function does not block regardless of the SO_LINGER setting on the socket.

An application should not rely on being able to reuse a socket after it has been shut down. In particular, a Windows Sockets provider is not required to support the use of connect on a socket that has been shut down.


很明显,msdn都要求shutdown之后立即closesocket,不要想恢复数据传输啦。如果你确实需要这样做的话,大概你就得重新改写你的思路了。
只有closesocket会释放资源,其他方法你就不要想啦。你这样写,我想应该会产生资源泄露,不过不知道MFC自身有没有保护措施:),没有的话,建议你把代码改写一下,呵呵


wj_xiazi 2003-08-27
  • 打赏
  • 举报
回复
回farfh(慕容长风) 兄:
我测试了,发现ShutDown之后,套接字的成员变量m_hSocket依然存在有效值。那就是说创建的那个socket对象并没有和CAsyncSocket对象分离。如果我没有把这个socket所占用的资源释放,那么每次循环都会有一个socket所占用的资源被浪费了。
我之所以要用如上的循环是因为我期望达到如下目的:
在程序运行中,套接字会被人为地终止接受数据。然后又人为地重新接收数据。
ShutDown可以达到这样地目的,但ShutDown之后我想不出方法来把它恢复接收数据。
那我只好Detch,然后在Create了。但我的套接字对象(从CAsyncSocket继承的对象)并不需要被Close掉。
我想问的是:closesocket把资源释放掉了,那Detach是否也释放了资源了呢?
farfh 2003-08-27
  • 打赏
  • 举报
回复

已经shutdown了应该无论怎么detach应该都没有任何意义了。
建议直接close
Detach本身不closesocket,所以你在这里Detach,没有任何意义。
wj_xiazi 2003-08-27
  • 打赏
  • 举报
回复
我们知道:Create创建了一个socket并把它和一个CAsyncSocket对象Attach 了一起。Detach把这个socket从CAsyncSocket对象分离开了同时把CAsyncSocket对象的成员变量m_hSocket赋NULL。这时socket所占用的资源释放了吗? 我是不是可以用如下的循环:

CAsyncSocket mySocket;
while(……)
{
mySocket.Create();
……
……
mySocket.ShutDown();
mySocket.Detach();

}
这样会造成一些资源得不到释放么?
SmallBigCat 2003-08-26
  • 打赏
  • 举报
回复
detach()是将CAsyncSocket对象释放,返回socket句柄,但不关闭socket,而Close()会将socket关闭掉;
ISAKEEPER 2003-08-26
  • 打赏
  • 举报
回复
帮你顶一下!

那位高手,有没有detach(),attach()的例子呀,另外开贴给分,绝对信用
farfh 2003-08-26
  • 打赏
  • 举报
回复
detach是取消对socket的一个引用,并将返回值赋予另外一个SOCKET对象。
与attach想对应
flinming 2003-08-26
  • 打赏
  • 举报
回复
Close是套接字断开处理事件

SOCKET Detach( );

Remarks

Call this member function to detach the SOCKET handle in the m_hSocket data member from the CAsyncSocket object and set m_hSocket to NULL.

18,356

社区成员

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

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