Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do if the port is already taken by another application?

Tags:

c#

port

I'm developing a server-client application. The server listens on some port (ex : 9090).

What to do if the computer is already using that port in another application? how do developers deal with it?

Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9090);
server.Bind(ep);
server.Listen(100);
like image 278
Murhaf Sousli Avatar asked Oct 15 '25 08:10

Murhaf Sousli


1 Answers

You immediately terminate your program with an error message that says that the port is already in use.

That's what everyone else does, too.

(Of course the port number should be configurable.)

like image 162
Kerrek SB Avatar answered Oct 16 '25 20:10

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!