Server broadcasts only to first client
up vote
0
down vote
favorite
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.
c sockets broadcast
add a comment |
up vote
0
down vote
favorite
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.
c sockets broadcast
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
c sockets broadcast
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.
c sockets broadcast
c sockets broadcast
asked Nov 11 at 16:05
Erik Vesterlund
207112
207112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
you can't have 2 processes bind on the same port. Not familiar with the broadcaster, but generally you have 2 options - either run the 2 processes on 2 machines on the same network or run the clients on separate ports and have the broadcaster broadcast on several ports
The command line when running 2 processes on 2 machines should be something like:
$ ./BroadcastSender 127.0.255.255 1337 hey &
when 127.0.255.255 is your subnet mask
--- edit(thanks @Jeremy) ---
you can also bind two sockets to the same UDP port using setsockopt
with SO_REUSEADDR/SO_REUSEPORT flags
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to callsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...])(and for BSD-based OS's, alsosetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before callingbind().
– Jeremy Friesner
Nov 11 at 19:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
you can't have 2 processes bind on the same port. Not familiar with the broadcaster, but generally you have 2 options - either run the 2 processes on 2 machines on the same network or run the clients on separate ports and have the broadcaster broadcast on several ports
The command line when running 2 processes on 2 machines should be something like:
$ ./BroadcastSender 127.0.255.255 1337 hey &
when 127.0.255.255 is your subnet mask
--- edit(thanks @Jeremy) ---
you can also bind two sockets to the same UDP port using setsockopt
with SO_REUSEADDR/SO_REUSEPORT flags
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to callsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...])(and for BSD-based OS's, alsosetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before callingbind().
– Jeremy Friesner
Nov 11 at 19:30
add a comment |
up vote
2
down vote
you can't have 2 processes bind on the same port. Not familiar with the broadcaster, but generally you have 2 options - either run the 2 processes on 2 machines on the same network or run the clients on separate ports and have the broadcaster broadcast on several ports
The command line when running 2 processes on 2 machines should be something like:
$ ./BroadcastSender 127.0.255.255 1337 hey &
when 127.0.255.255 is your subnet mask
--- edit(thanks @Jeremy) ---
you can also bind two sockets to the same UDP port using setsockopt
with SO_REUSEADDR/SO_REUSEPORT flags
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to callsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...])(and for BSD-based OS's, alsosetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before callingbind().
– Jeremy Friesner
Nov 11 at 19:30
add a comment |
up vote
2
down vote
up vote
2
down vote
you can't have 2 processes bind on the same port. Not familiar with the broadcaster, but generally you have 2 options - either run the 2 processes on 2 machines on the same network or run the clients on separate ports and have the broadcaster broadcast on several ports
The command line when running 2 processes on 2 machines should be something like:
$ ./BroadcastSender 127.0.255.255 1337 hey &
when 127.0.255.255 is your subnet mask
--- edit(thanks @Jeremy) ---
you can also bind two sockets to the same UDP port using setsockopt
with SO_REUSEADDR/SO_REUSEPORT flags
you can't have 2 processes bind on the same port. Not familiar with the broadcaster, but generally you have 2 options - either run the 2 processes on 2 machines on the same network or run the clients on separate ports and have the broadcaster broadcast on several ports
The command line when running 2 processes on 2 machines should be something like:
$ ./BroadcastSender 127.0.255.255 1337 hey &
when 127.0.255.255 is your subnet mask
--- edit(thanks @Jeremy) ---
you can also bind two sockets to the same UDP port using setsockopt
with SO_REUSEADDR/SO_REUSEPORT flags
edited Nov 11 at 19:40
answered Nov 11 at 16:14
Shlomi Agiv
625313
625313
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to callsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...])(and for BSD-based OS's, alsosetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before callingbind().
– Jeremy Friesner
Nov 11 at 19:30
add a comment |
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to callsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...])(and for BSD-based OS's, alsosetsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before callingbind().
– Jeremy Friesner
Nov 11 at 19:30
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to call
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...]) (and for BSD-based OS's, also setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before calling bind().– Jeremy Friesner
Nov 11 at 19:30
Actually you can bind two sockets to the same UDP port, but in order to do it you'll have to call
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, [...]) (and for BSD-based OS's, also setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, [...]) on the socket(s) before calling bind().– Jeremy Friesner
Nov 11 at 19:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250574%2fserver-broadcasts-only-to-first-client%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown