注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

socket不能shutdown之后重复使用,每次连接都要使用一个新的socket才行  

2013-10-26 15:39:57|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

一个短连接的客户端,要发起大量的连接,发现创建连接的代价其实还挺大。本来是想说这个socket能不能重复使用,避免创建的开销。结果发现应该是不行的,应该每次创建才行啊。

msdn中的文档摘录

connect函数

http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx

When a connection between sockets is broken, the socket that was connected should be discarded and new socket should be created. When a problem develops on a connected socket, the application must discard the socket and create the socket again in order to return to a stable point.


shutdown 函数

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481(v=vs.85).aspx

Once the shutdown function is called to disable send, receive, or both, there is no method to re-enable send or receive for the existing socket connection.

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.

If an application wants to reuse a socket, then the DisconnectEx function should be called with the dwFlags parameter set to TF_REUSE_SOCKET to close a connection on a socket and prepare the socket handle to be reused. When theDisconnectEx request completes, the socket handle can be passed to the AcceptEx or ConnectEx function.

If an application wants to reuse a socket, the TransmitFile or TransmitPackets functions can be called with the dwFlagsparameter set with TF_DISCONNECT and TF_REUSE_SOCKET to disconnect after all the data has been queued for transmission and prepare the socket handle to be reused. When the TransmitFile request completes, the socket handle can be passed to the function call previously used to establish the connection, such as AcceptEx or ConnectEx. When the TransmitPackets function completes, the socket handle can be passed to the AcceptEx function.


DisconnectEx function

http://msdn.microsoft.com/en-us/library/windows/desktop/ms737757(v=vs.85).aspx

The DisconnectEx function closes a connection on a socket, and allows the socket handle to be reused.

Note  This function is a Microsoft-specific extension to the Windows Sockets specification.


还是老老实实每次socket()创建,shutdown() close()吧

  评论这张
 
阅读(720)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017