Dedicated network port in C #

How can I ensure that a particular port on my computer is not being used for any other purpose other than the service I need in C #?

In my lan-messenger simulator I am getting a denied error when trying to connect to a remote host. I need to somehow make sure the port is not being used for any other purpose.

0


a source to share


3 answers


You cannot guarantee this. Any program can register to listen on any port and the OS will allow it if no other process is using it.



Your best bet is to choose a port that is known to be used by some service and use that as your incoming port. Here is a list of known ports.

+3


a source


IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, 0);

      

This will give you the first available port on the local machine.



http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

+1


a source


Open tcp-socket listening for it? This forces someone else to do the same.

The connection refused sounds more like a firewall problem.

0


a source







All Articles