nyyrikki
nyyrikki

Reputation: 1486

C / how to bind multiple sockets for incoming and outgoing data

I discovered a problem in my code when I try to bind multiple sockets. I try to explain the situation, I have an application consisting of two parts, one is written in C running on machine_1, the other one is in Java running on machine_2. The C program should be able to send udp-packets through one port to 4 ports on the machine_2 which is listening on them. Vice versa machina_2 should send udp-packets through one outgoing port to 4 corresponding ports on machine_1.

The problem I have is on machine_1:

  1. I have two threads there, one processing outgoing data to machine_2 and one processing the incoming data. Within the first thread (processing outgoing data) I create 4 sockets (with 4 different ports) to address the corresponding sockets on machine_2. Works fine in that direction...
  2. But when I try to create the sockets for the incoming data from machine_2, and I want to use the same port numbers as I did for incoming data on machine_2, but then I run into problems when I try to bind the sockets. Giving me the : Address already in use error.

I need some help on how to set up the socket configuration on machine_1 inside the C application for outgoing and incoming ports. And maybe an example on how to use the select() function to listen on all 4 incoming sockets.

If come code examples needed, I can add them here, but as I said it is only working for the thread_1 which handles the outgoing data through one socket to 4 different sockets on machine_2.

Thanks in advance for your help!

I hope the attached picture can explain the situation little bit more, important is that I want to use the same port numbers on both machines for incoming data. Means, port_1 on machine_1 has same port number than port_1 on machine_2, and so on...port_configuration

Upvotes: 1

Views: 1913

Answers (2)

Fred
Fred

Reputation: 17085

My advise is to just use 4 sockets. Sockets are bidirectional (that is if you really need 4 sockets, since this is UDP, one socket would probably be enough). Then you can share the sockets among several threads and read or write to them. You just need to be sure to synchronize whatever needs to be synchronize.

Here's an example on how to use select:

http://www.lowtek.com/sockets/select.html

Upvotes: 1

aztaroth
aztaroth

Reputation: 989

Create one socket for sending, then four for receiving, bind the receiving sockets to the correct local ports. Use sendto on the outgoing socket to send stuff to the receiver.

Lazy example for select (probably buggy, but you should get the general idea):

fd_set fds;
int nfds = /* max(in_1, in_2, in_3, in_4) + 1 */;
while(1) {
    int ret = 0;
    FD_ZERO(&fds);
    FD_SET(in_1, &fds);
    FD_SET(in_2, &fds);
    FD_SET(in_3, &fds);
    FD_SET(in_4, &fds);
    ret = select(nfds, &fds, NULL, NULL, NULL);
    if(ret < 0) {
        break; /* something went wrong */
    } else if(ret == 0) {
        continue; /* nothing to do, try again */
    }
    if(FD_ISSET(in_1, &fds)) {
        handle_in_1();
    }
    if(FD_ISSET(in_2, &fds)) {
        handle_in_2();
    }
    /* ... */
}

Upvotes: 3

Related Questions