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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 16:05









      Erik Vesterlund

      207112




      207112
























          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






          share|improve this answer























          • 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













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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






          share|improve this answer























          • 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

















          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






          share|improve this answer























          • 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















          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






          share|improve this answer














          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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


















          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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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