Hi all.
Im having a problem with my project.
I need that my socket recive multiple conections from differents pcs.
Example:
I have my socket, it is listening, when a pc connect to its, it stop listen, so there come the second computer trying to connect, and this computer cant doit beacuse socket isnt listening anymore.
I tried, putting the socket to listen, when anybody connects to it, the code makes another socket and it puts it in listen mode. The problem is that when i run the program there is an error and it shows that i cant have 2 sockets on the same port.
Thanks

Dani AI

Generated

As noted, the right approach is to keep one listening socket on the port and have that listener hand each incoming connection off to a worker. Creating a second listener bound to the same port causes the OS error you saw — only one socket can bind to a given address/port combination at a time. Good choice moving to TcpListener, because it wraps the listen/accept pattern cleanly.

A simple, modern pattern is: Start one TcpListener, loop on Accept, and hand each accepted TcpClient to a task/worker so the listener can immediately go back to accepting. Example (async-friendly):

var listener = new TcpListener(IPAddress.Any, 5000);
listener.Start(); // optionally: listener.Start(backlog);

while (!cancellationToken.IsCancellationRequested)
{
    var client = await listener.AcceptTcpClientAsync().ConfigureAwait(false);
    _ = Task.Run(() => HandleClientAsync(client, cancellationToken));
}

HandleClientAsync should close/dispose the TcpClient when finished and use NetworkStream.ReadAsync/WriteAsync. For small numbers of clients a thread-per-connection is fine; for many simultaneous connections prefer async IO or a pattern based on SocketAsyncEventArgs/IOCP to avoid thread exhaustion.

Troubleshooting and tips: use listener.Start(backlog) to control the OS accept queue; call listener.Stop() or cancel the loop to shut down cleanly; if you get "address already in use," check for a leftover process with the port (netstat -ano on Windows) or a previous run in TIME_WAIT. Windows’ reuse semantics differ from Unix, so don’t rely on SO_REUSEADDR to let two listeners bind the same port. For production servers consider timeouts, connection limits, and proper exception handling to keep the accept loop robust.

Thanks to and for the pointers — the TcpListener accept-and-delegate pattern is the simplest, most reliable fix for this class of problem, which is what implemented.

Recommended Answers

All 3 Replies

should help you.

The basic idea is this:

Each port can only have 1 passive listening socket, but many active sockets.

You code must listen for connections on the port, when one is detected, it must create a new socket to handle the active connection and then go back to passively listening for another connection.

Google will give a lot of code examples of this.

Thanks i could do it with a tcplistener.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.