Sign in to follow this  
eran65

Socket.Disconnect( )

Recommended Posts

Socket.Disconnect(bool reuse) according to msdn enables the 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,what i'm asking is,
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 the all purpose is to reuse the socket what would this grant me ?

Share this post


Link to post
Share on other sites
1) No, any. It reuses Net.Socket instance, not kernel/OS sockets. This is unrelated to SO_REUSEADDR.

2) It saves creating a new instance of Net.Socket. Kernel still needs to go through the whole thing, sockets cannot be manipulated from user land

3) Perhaps in a case where socket instance has been passed to different parts of application. If connection is lost or needs to be reestablished, rather than creating a new instance and passing it all around, one would just reconnect existing instance.

Share this post


Link to post
Share on other sites
ok
now lets say we Close the socket on the client end when the program closes and i want to save the instance of the socket on the servers end
how would i save that instance , do i have to keep track of the disconnected instance myself ?

in one place in the program


Socket new_socket ;

while(true)
{
new_socket = server.Accept() ;
}


in a diffrent place
{
.
.
some_socket.ShutDown(ShutDown.Both) ;
some_socket.Disconnect(true) ;
.
.
}

should i save the instance and use it in the accpet next time ?

also do i have to use shutdown ?
or do my buffers automaticly flush ?

Share this post


Link to post
Share on other sites
[quote name='eran65' timestamp='1298043016' post='4775902']
now lets say we Close the socket on the client end when the program closes and i want to save the instance of the socket on the servers end
how would i save that instance , do i have to keep track of the disconnected instance myself ?
[/quote]

Yes, you have to keep track of the disconnected instance if you want to re-use it.

However, there really is no reason to worry about doing this. Pretty much the only case where this matters is when serving a very high rate of static page requests through HTTP/1.0 that doesn't allow persistent connections. In that case, the GC cost of continually creating and collecting socket instances at the C# level (tens of thousands per second) actually is worth something. If you're doing a game server where connections persist for some time (even 15 seconds counts as "persisting" here), then that would be a mostly meaningless "optimization."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this