Hi. I know how to validate a port number if it's in the min or max range. But how do you test for if the port number is being currently used?
Printable View
Hi. I know how to validate a port number if it's in the min or max range. But how do you test for if the port number is being currently used?
I don't think you can unless you write a component that works at the level a firewall would.
Ports can be used by multiple programs at once. You can have 1 FTP program connected to 3 seperate FTP server all utilizing Port 21.
Why would you need this?
If you try to bind a TCP socket to a port that is already is associated with a TCP socket, it will fail. Same for UDP (I think). Of course, it might also fail for a myriad of other reasons.
i got the answer. Create a socket and bind onto the port using localhost as the ip address. If a SocketException is thrown then the port number is using. If ArgumentOutOfRangeException is thrown, then the port number is not between min and max.
Jennifer.