0

I have a server application, part of its code like this:

socket = socket(...); // create a socket
setsockopt(RE_USEADDR); // set the socket option RE_USEADDR
bind(socket, 127.0.0.1, 8080); // bind the socket to the IP address and Port
listen(); // listen

Here is the question:

  1. I launch the server application twice, on the same machine, same user. Both of them can work well.
  2. Launch the client application, which connects to 127.0.0.1:8080
  3. Send some contents to the server. However only one of the server application can receive the message from client.

Is there anyway to make sure the two server applications all receive the message from client. If there is, please explain how and why in TCP and UDP.

Thank you very much

pptaszni
  • 3,794
  • 5
  • 19
  • 37
Kuroto
  • 3
  • 3
  • 2
    Have a front end server application that forwards the message to the two other server applications. – Eljay Sep 01 '20 at 15:26
  • How can you launch 2 servers that listen on the same port? You should get some kind of "error, address already in use". Or maybe I don't understand `RE_USEADDR` option ... – pptaszni Sep 01 '20 at 15:29
  • @pptaszni Yes, you can if you use the RE_USEADDR option for the socket – Kuroto Sep 01 '20 at 15:33
  • Yes, true. However, apparently the behavior of 2 sockets bound to the same address is non-deterministic, according to [this docs](https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse). They also mention the possibility to use multicast – pptaszni Sep 01 '20 at 15:40
  • which OS are you using ? – Cyril Jouve Sep 01 '20 at 15:42
  • That's not what SO_REUSEADDR does. q.v. https://stackoverflow.com/a/14388707/4641116 ... is RE_USEADDR the same as SO_REUSEADDR? – Eljay Sep 01 '20 at 15:42
  • @Eljay yes, SO_REUSEADDR, I just write a sample code that maybe incorrect and try to explain what i want to know. – Kuroto Sep 01 '20 at 15:46
  • @Cyril Jouve Win10 home – Kuroto Sep 01 '20 at 15:46
  • @pptaszni Yes, I want to use the multicast, but does that only work for UDP? – Kuroto Sep 01 '20 at 15:49
  • 1
    @Kuroto You can't have a TCP client socket connected to multiple TCP servers at a time, let alone send a message to multiple TCP servers simultaneously. You would need multiple TCP connections for that. There is no point in binding multiple TCP servers to the same local IP/Port, except maybe for load balancing. And there is no multicast in TCP – Remy Lebeau Sep 01 '20 at 17:12
  • You can also look into multicast, if you are looking to have multiple processes listen on the same port. – Alex Baum Sep 01 '20 at 20:23

0 Answers0