FlappySocks
FlappySocks

Reputation: 3880

C# Asynchronous socket server, temporary stop new connections

I have a C# Asynchronous socket server, like so:

private Socket _serverSocket;
_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 1234));
_serverSocket.Listen(6);
_serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);

I want to be able to stop new connections, without effecting existing connections, and then maybe allow new connections later. What is the correct way to do it?

Upvotes: 0

Views: 930

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 457302

This should do it:

_serverSocket.Close();

When you want to start listening again, re-create the socket, bind it, listen, and begin accepting again.

Upvotes: 1

Related Questions