If you try to open a listening socket (that is - a socket that other clients can connect to), you need to provide a port number (because otherwise the clients won't know which port to connect to). If that socket is already in use, the opening will fail. You'll get an error and no-one will be able to connect with your program.
If you want to open a socket to connect to a (possibly remote) listening port, you should get assigned a random free local port number by the system and you'll be fine until you run out of ports (or open handles).
I'm not sure if you actually can open a connection to a listening port from a port number that is already in use as a listening port. I also can't think of a situation where you'd care.
| [reply] |
And for completeness, each IP address has it's own range of ports. i.e. if you have a server listening on 127.0.0.1:3306, you could still open a listening socket on 192.168.1.1:3306 (assuming the box has a network interface with that IP).
Commonly, applications specify 0.0.0.0 as the IP address to listen on, which means "the IP addresses of all interfaces, which are currently up".
In an environment where network interfaces go and up and down, this can be a real issue, since the number of interfaces your 0.0.0.0 server will listen on depends when it is started.
| [reply] |