Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: UDP Network help with ports  (Read 2265 times)

0 Members and 1 Guest are viewing this topic.

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
UDP Network help with ports
« on: October 15, 2012, 05:19:56 am »
I've got a problem when it comes to ports.
I've got my server binded to port: 4567
My client is binded to port: 4568

There's a problem with this.
If I launch one client, it will successfully bind to port 4568.
If I launch another client, it will fail to bind to port 4568.

So how exactly would I launch two clients on the same computer?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: UDP Network help with ports
« Reply #1 on: October 15, 2012, 08:03:37 am »
You must use one different port per client. You can't bind two sockets to the same port on the same network interface.
Laurent Gomila - SFML developer

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: UDP Network help with ports
« Reply #2 on: October 15, 2012, 04:32:54 pm »
Yes but how do I tell whether a port is all ready binded?
i.e.

Client 1 checks if port 4568 is binded...No.
Client 1 binds to port 4568.
Client 2 checks if port 4568 is binded...Yes.
Client 2 checks if port 4569 is binded...No.
Client 2 binds to port 4569.

Now I read the documentation and it says I can use: "Socket::AnyPort".
However I want to know if this is safe?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: UDP Network help with ports
« Reply #3 on: October 15, 2012, 04:47:16 pm »
Quote
Yes but how do I tell whether a port is all ready binded?
There are two ways:
- the bind() function returns an error
- you store the list of the ports that you already use (this will however not tell you which ports are used by other applications)

Quote
Now I read the documentation and it says I can use: "Socket::AnyPort".
However I want to know if this is safe?
This is the only safe method to get a free port, because the OS chooses it for you. However, since the port is not known in advance, you'll need to transmit it to applications that want to send something to your socket.

Usually, clients don't need to use a known port, only the server needs to. So using Socket::AnyPort is the typical solution to your problem.
Laurent Gomila - SFML developer

Qix

  • Full Member
  • ***
  • Posts: 139
  • I am Qix!
    • View Profile
    • Natoga Technologies
Re: UDP Network help with ports
« Reply #4 on: October 20, 2012, 09:55:03 am »
Don't bind to a port on the client side. The system will find a suitable port.

--Snip--

Totally just realized this was about UDP, not TCP.

.. Time for sleep.
~ Qix
Creator of Rippl Studio
Code: [Select]
<danharibo> iostream: I don't do enough drugs to think that's a good idea.

 

anything