Advertisement

Choosing a port

Started by October 07, 2014 04:40 PM
7 comments, last by viper110110 10 years, 1 month ago

I am trying to do a client/server setup and naturally I had to choose a port for each of them. The server one has been fine but the client one has given me trouble on several occasions. I get the exception:

SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

I understand that the port has been taken, but every time I end up googling the port I was using I end up finding some obscure thing that happened to be running while trying to run my game. Rather than trying new random ports every time this happens, is there a sure way to find myself a port that won't be used for anything? Any of the lists I can find of used ports always seem to show my ports as unassigned.
Alternatively, would it be a good idea to, in code, try a random port until I get one that works?
Is this TCP or UDP?
For TCP, you should not bind the client explicitly. If the client calls connect(), then let the OS pick a port for you.
So, a server is something that calls bind(), listen(), and accept(). A client is something that calls connect().
enum Bool { True, False, FileNotFound };
Advertisement

Is this TCP or UDP?
For TCP, you should not bind the client explicitly. If the client calls connect(), then let the OS pick a port for you.
So, a server is something that calls bind(), listen(), and accept(). A client is something that calls connect().

This is UDP. Server opens itself on 10124 and waits for stuff to come in. Client opens itself on 10123 and then connects to 10124.

What about just trying out ports until you find one?

What about just trying out ports until you find one?

Do you mean in code or by hand? The problem with doing it by hand is that it sometimes takes a while to show up. I've been using 10123 for a few months now without issue and only today did it become an issue. I tried restarting and everything else that would clear it out if I still had it open. I could do it in code but I won't personally know which port it is, and I'm not sure if that could cause issues.

Is this TCP or UDP?
For TCP, you should not bind the client explicitly. If the client calls connect(), then let the OS pick a port for you.
So, a server is something that calls bind(), listen(), and accept(). A client is something that calls connect().


This is UDP. Server opens itself on 10124 and waits for stuff to come in. Client opens itself on 10123 and then connects to 10124.


why does the client care about which port it's using?
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
Advertisement
Yes, even on UDP, the client doesn't need to choose a port. The OS will pick a free one the first time you call sendto().
enum Bool { True, False, FileNotFound };

Yes, even on UDP, the client doesn't need to choose a port. The OS will pick a free one the first time you call sendto().


and the server will get the ip and port to reply to from the recvfrom function.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

client = new UdpClient();

Well, that was easy. Thanks.

This topic is closed to new replies.

Advertisement