SFML community forums
Help => Network => Topic started by: Grimlen 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?
-
You must use one different port per client. You can't bind two sockets to the same port on the same network interface.
-
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?
-
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)
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.
-
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.