开发者

Socket.Disconnect(bool reuse)

Socket.Disconnect(bool reuse) according to MSDN enables the开发者_如何学JAVA program to reuse the socket. My questions are:

  1. If we decide to reuse the socket does that mean it will reuse the same local end point as well? Would it preserve the port for that socket or would it just (I don't believe it does) save all the objects resources?

  2. If in effect it does only save the resources would they be allocated to the next connection and if so do the send/receive buffers automaticly flush or is there some way the flush them? Does Socket.ShutDown(ShutDown.Both) do that for me on disconnect?

  3. In what situation would I use Socket.Disconnect(false)? If all the purpose is to reuse the socket what would this grant me?


  1. If reuse = false the socket will be disposed automatically after closing. All settings as EndPoint and Buffer sizes are not reseted to defaults, but you can change it if you desire. Supose the user decides to change the connection port. You just need to close the socket, change the port and connect the socket again without create a new socket and set it up again.

  2. The Socket.Shutdown just disables send/receive/both; not disconnect the socket. However this occurs for new incoming/outgoing data so buffers will be flushed normaly. It is a good practice to call it before disconnection to ensure all data was sent and no data is being received while disconnecting.

  3. Returning to the example of 1. Supose you need to change only the port. You dont want to create the socket again, configure all parameters of the socket as events and buffers again, etc. It consumes time and resources, so it is better to reuse the socket you already have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜