The correct way to stop listening on a socket

I have a server that listens for a socket connection:

public class Server { private Socket _serverSocket; public Server() { _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 1234)); _serverSocket.Listen(1); } public void Start() { _serverSocket.BeginAccept(HandleAsyncConnectionMethod, null); } public void Stop() { //????? MAGIC ????? } //... rest of code here } 

What is the correct (clean) way to close a socket?

Is it enough to call:

 _serverSocket.Disconnect(true); 

in the Stop () method? or is there any other work that needs to happen to close the connection cleanly?

+17
c # sockets
Jan 16 '09 at 22:47
source share
7 answers

Terminating a TCP connection correctly includes a four-way handshake. You want both ends to tell the other that they are shutting down and then confirming each other.

Wikipedia explains this process: http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_termination

This post explains how to do this in C #: http://vadmyst.blogspot.com/2008/04/proper-way-to-close-tcp-socket.html

+3
Jan 16 '09 at 23:20
source share

Since you are listening for incoming TCP connections, you can use System.Net.Sockets.TcpListener , which has a Stop () method. However, it does not have asynchronous operations.

+2
Jan 16 '09 at 23:03
source share

2 ways to close it without exception

1) create a temporary connection socket and connect it to the listening so that it can run its handler, and then just end it using a regular EndAccept and after that close both.

2) just close (0) the listening socket, which will lead to a false positive of its callback, if you then look into your auditory socket, you will see that its state is โ€œclosedโ€ and โ€œlocatedโ€. This is why calling EndAccept throws an exception. You can simply ignore it and not call EndAccept. The listening socket immediately disconnects without a timeout.

+1
Aug 10 '13 at 17:53
source share

The cleanest way to immediately terminate an Accept call is to call _serverSocket.Dispose (); Any other method calls like Shutdown or Disconnect raise an exception.

0
Dec 19 '16 at 10:30
source share

You should use Socket.Shutdown () and then Socket.close () . Socket.Disconnect () is usually used only if you intend to reconnect the same socket.

-one
Jan 16 '09 at 23:23
source share

First, you need to make sure that you are monitoring any client sockets created in the BeginAccept process. First close them using the Socket.Shutdown () and Socket.Close () methods. Once they have all been closed, do the same in the listening jack itself.

-one
Jan 17 '09 at 0:23
source share

This should handle this ... but if you need to be absolutely sure, you can always kill him with fire:

Not for sockets, but the same idea applies. , which should close it in every possible way, and then finally set the socket to null.

-2
Jan 16 '09 at 22:58
source share



All Articles