Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding 40 Sockets to 40 different IP addresses

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.

like image 412
Ayse Avatar asked Apr 12 '13 10:04

Ayse


People also ask

What does it mean to bind an IP address?

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.

Why do we bind IP?

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.

How do I bind to all available IPv4 addresses?

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.

What are the different types of address binding?

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.

What does binding a socket mean?

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).

When will the address binding be done in a program?

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.


2 Answers

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.).

like image 37
Sander De Dycker Avatar answered Sep 29 '22 05:09

Sander De Dycker


One common way to do this with UDP is:

  • Server bind() to a well known port.
  • Client sends the initial packet to that well known port
  • Server receive the first packet from a client on the well known port.
  • Server creates a new socket with a random port
  • Server replies to the client from this new socket.
  • Client receives the reply, notices it comes from another port than the well known server port, and uses that port as the destination for further communication.

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.

like image 159
nos Avatar answered Sep 29 '22 04:09

nos