I am writing on UDP server/client application.
I want my single server to handle 40 clients at a time. For this, I want to create 40 dedicated threads, each dedicated for one single client. Since there are 40 threads one for each client, I want to create 40 dedicated sockets as well.
But the problem that:
I don't know what will be the 40 IP addresses to which I shall bind() my sockets. (since as far as I now, I have to bind() to my Server\s IP address.) Normally I bind() to "INADDR_ANY"
when there is only single socket.
But what should be the IP addresses at which I should bind() each of my 40 sockets? Please help me. Any comment/ help is appreciated.
When you run a server on a machine it listens for incoming client connections. Administrators can selectively pick which IP addresses a server process listens on. This selective picking is called binding. For example, if you just bind to the loop-back, clients running on the same machine can connect to the server.
If you bind a socket for receiving data to a specific address you can only receive data sent to this specific IP address. For example, if you bind to 127.0. 0.1 you will be able to receive data from your own system but not from some other system on the local network, because they cannot send data to your 127.0.
If you want to bind to all available IPv4 addresses, specify 0.0.0.0 as your IP address. If you're behind a router and wish to have your socket internet-accessible, rather than just available on your LAN, you'll need to set up a port forwarding rule so that users outside your LAN can access the service.
In this article, We are going to cover address binding with the help of an example and Its types like compile time, load time, and execution time address binding. Let’s discuss one by one.
Show activity on this post. Binding of a socket is done to address and port in order to receive data on this socket (most cases) or to use this address/port as the source of the data when sending data (for example used with data connections in FTP server).
It will be done after loading the program into memory. This type of address binding will be done by the OS memory manager i.e loader. It will be postponed even after loading the program into memory. The program will be kept on changing the locations in memory until the time of program execution.
bind
only needs the local address, not the remote address.
If you want one socket for each client, then you'll need to use different ports for each (using bind
). That way, each client can send its traffic to a dedicated port, and you can have a thread for each socket/port.
It's probably a better idea to only have one socket (and one port) though, and have logic in your code to assign traffic to a thread based on the remote address (retrieved using recvfrom
eg.).
One common way to do this with UDP is:
You'll use the getpeername() call to learn the remote address.
Keep in mind that UDP is connection-less, you'll need some way to signal the end or time out you sockets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With