Jump to content
  • Advertisement
Sign in to follow this  
viper110110

Choosing a port

This topic is 1476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 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.

Share this post


Link to post
Share on other sites

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? Edited by slicer4ever

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!