I'm trying to broadcast a message from a server to all clients, but only one client receives the message.
I want to run this server and two or more instances of this client (taken from Donahoo, Calvert, "TCP/IP Sockets in C", 1e; I can paste the code into this question on request).
The programs work fine with a single client, but when running two clients only one (the first) ever receives a message, while the second instance just gets stuck (on bind
).
I don't know what I'm doing wrong, I'm sure the program is correct so perhaps I'm running it wrong. I start the server as:
$ ./BroadcastSender localhost 1337 hey &
As for the clients, I have tried two variations, the first:
$ ./BroadcastReceiver 1337 & ./BroadcastReceiver 1337 &
In the second variation I have added while (1) {}
after close(sock)
and then run as:
$ ./BroadcastReceiver 1337 &
$ ./BroadcastReceiver 1337 &
Both variations give the same result, namely that the first client receives the message, the other one doesn't, but instead gets stuck trying to bind
.
Am I running the server/clients the wrong way, or is there something missing in the code? I'm new to sockets so I don't really see if there's anything in, say, the server code saying "I'm gonna broadcast to one client only".
Could you give me some pointers in the right direction? There are other questions and answers about broadcasting but I haven't found one that addresses this particular issue. Thank you.
转载于:https://stackoverflow.com/questions/53250574/server-broadcasts-only-to-first-client