views:

515

answers:

6

i am studying asynchronous C# Sockets at the moment and i have noticed something that i am confused about.

in all the End...(Accept/Connect) etc there is a code section like:

Socket s = (Socket) ar.AsyncState;

here it is being casted as a socket.

as each End uses these local Sockets is there any point in trying to close() any of them once i am done?

what is the best practice?

thank you.

+2  A: 

I think the best practice for sockets is similar to that for the streams. I personally always use the close() methods in both - sockets and streams. If something has been opened, it should be properly closed :)

brozo
+1  A: 

It will get closed eventually even if you don't (when the garbage collector kicks in), however best practices dictates that you close handles to objects when you no longer need them.

Consider file streams, not closing a file stream can mean that data will not be wrote to a file. Sutble problems like this can occur if you do not explicity close the handle of objects that should be closed.

Finglas
+1  A: 

Closing your sockets is critical. Just because you don't notice any ill effects in a particular Framework version doesn't mean in future implementation the Framework might leave resources in use until it is closed. You most likely are leaving resources in use on the system as well. Plus, it doesn't hurt. :p

The object probably does free its resources and implictly close the socket when it is destroyed, but with a automatic garbage collected environment like C#, you never know when that might happen.

David Pfeffer
+4  A: 

Most authors seem to agree that if something implements IDisposable, you should call Dispose. The easiest way to do this is to use 'using' which automatically calls Dispose for you.

using (DirectoryEntry de = new DirectoryEntry(path))
{
.... some code
}
serialhobbyist
+1  A: 

Either by using the

using(Socket s)
{
...
}

or by using socket open and at the end closing the socket.

If you leave a socket opened and try to open it again somewhere else then a runtime error will occur... All streams dataReaders, connections must be closed. the main reasons seem to be: 1. security 2. waste of memory 3. prone to runtime errors

GxG
+1  A: 

I suggest either Disposing or Closing the stream after you are done. This allows the system to immediately free the network resources once you are done with your socket.

If you are using the socket from a single function, the using statement works well as it implicitly disposes your object. Otherwise (for example when keeping a socket active, maintaining a reference to it in an object), you will want to either call Close() or Dispose() when you are done. Otherwise, the socket could stay open indefinitely. Dispose() may be a slightly better choice because it frees more resources than a Close() would.

MSDN may skip the Close because the socket will be automatically closed at the termination of the process when the remaining resources are garbage collected.

Pigrew