0

I am working with two binaries which use UDP sockets. Process A waits for messages on a UDP socket (IP_1:PORT_1) by select(), and process B eventually sends through an UDP socket.

By some constraints out of scope, process B needs to send by a socket on (IP_1:PORT_1). Since this is the same IP:PORT pair for both processes, it is not possible to use bind(). I tried SO_REUSEADDR, but I am wondering if reusing the IP:PORT with SO_REUSEADDR for sending and receiving makes sense, or was this option conceived just for listening sockets?

process A

int nOptVal = 1;
setsockopt(UDPSocket, SOL_SOCKET, SO_REUSEADDR, &nOptVal, sizeof(nOptVal));
bind(UDPSocket, (struct sockaddr *)&addrLocal, sizeof(addrLocal));

select(fdMax+1, UDPSocket, NULL, NULL, NULL);

process B

int nOptVal = 1;
setsockopt(UDPSocket, SOL_SOCKET, SO_REUSEADDR, &nOptVal, sizeof(nOptVal));
bind(UDPSocket, (struct sockaddr *)&addrLocal, sizeof(addrLocal));

sendto(UDPSocket, buff, len, 0, (struct sockaddr *)&addrDest, sizeof(struct sockaddr));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Javi
  • 1
  • 4
  • 3
    The SO_REUSEADDR option typically makes most sense for passive connection-oriented sockets (like a TCP server socket). – Some programmer dude Mar 01 '22 at 15:46
  • 2
    The problem with this setup is that one can use setsockopt, but messages will be send to either one of the sockets. So process A will not be able to reliable receive messages (as much as the non-reliability of UDP would allow anyway). – Steffen Ullrich Mar 01 '22 at 15:48
  • Just what I was afraid of, @SteffenUllrich, thank you. So although is possible this setup, doesn't make sense because I loss messages. – Javi Mar 02 '22 at 06:49
  • @Someprogrammerdude, so then I would say that the setup I propose is not correct due to process A will loss messages, and ONLY makes sense for passive connection-oriented – Javi Mar 02 '22 at 06:51

0 Answers0