Socket broadcasting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rezamirani
    New Member
    • Aug 2007
    • 6

    Socket broadcasting

    Dear Mr,

    I wrote one sample socket in linux, it broadcast an character to all the machines in my subnet.
    I set socket options in udp to broadcast then sendto() the client, but I couldn't receive any thing in client.
    I checked my program with several programs, it's same.but I can't receive from broadcast.
    What should I do ?

    Thanks for you incoming helps.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    What did you write your program in?

    Can you ping the machines from the computer your program is running on?

    What programs did you "check yours against" and how did you check it?

    Are you aware of the Networking definition of 'broadcast' and how that's different depending on your situation?

    Comment

    • rezamirani
      New Member
      • Aug 2007
      • 6

      #3
      Originally posted by sicarie
      What did you write your program in?

      Can you ping the machines from the computer your program is running on?

      What programs did you "check yours against" and how did you check it?

      Are you aware of the Networking definition of 'broadcast' and how that's different depending on your situation?
      Dear Sicarie,
      This is my program on both server and client side , please check and tell me more about Network definition od broad cast and different depending on my situation.

      >>>>>>>>>>>>>>> >>>>>>>>>>>>>Se rver Side>>>>>>>>>>> >>>>>>>>
      [code=cpp]
      #include <stdio.h>
      #include <sys/socket.h>
      #include <arpa/inet.h>
      #include <stdlib.h>
      #include <string.h>
      #include <unistd.h>
      #include <netinet/in.h>

      #define BUFFSIZE 255
      void Die(char *mess) { perror(mess); exit(1); }

      int main(int argc, char *argv[]) {
      int sock;
      struct sockaddr_in echoserver;
      struct sockaddr_in echoclient;
      char buffer[BUFFSIZE];
      unsigned int echolen, clientlen;
      int received = 0;

      if (argc != 4) {
      fprintf(stderr, "USAGE: %s <server_ip> <word> <port>\n", argv[0]);
      exit(1);
      }
      /* Create the UDP socket */
      if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
      Die("Failed to create socket");
      }
      /* Construct the server sockaddr_in structure */
      memset(&echoser ver, 0, sizeof(echoserv er)); /* Clear struct */
      echoserver.sin_ family = AF_INET; /* Internet/IP */
      echoserver.sin_ addr.s_addr = inet_addr(argv[1]); /* IP address */
      echoserver.sin_ port = htons(atoi(argv[3])); /* server port */

      /*Set socket option to broadcast*/
      int opt = 1;
      setsockopt(sock (), SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int));

      /* Send the word to the server */
      echolen = strlen(argv[2]);
      if (sendto(sock, argv[2], echolen, 0,
      (struct sockaddr *) &echoserver,
      sizeof(echoserv er)) != echolen) {
      Die("Mismatch in number of sent bytes");
      }

      close(sock);
      exit(0);[/code]
      <<<<<<<<<<<<<<< <<<<<<in other side program >>>>>>>>>>>>>>> >>
      [code=c]
      #include <stdio.h>
      #include <sys/socket.h>
      #include <arpa/inet.h>
      #include <stdlib.h>
      #include <string.h>
      #include <unistd.h>
      #include <netinet/in.h>

      #define BUFFSIZE 255
      void Die(char *mess) { perror(mess); exit(1); }

      int main(int argc, char *argv[]) {
      int sock;
      struct sockaddr_in echoserver;
      struct sockaddr_in echoclient;
      char buffer[BUFFSIZE];
      unsigned int echolen, clientlen, serverlen;
      int received = 0;

      if (argc != 2) {
      fprintf(stderr, "USAGE: %s <port>\n", argv[0]);
      exit(1);
      }
      /* Create the UDP socket */
      if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
      Die("Failed to create socket");
      }
      /* Construct the server sockaddr_in structure */
      memset(&echoser ver, 0, sizeof(echoserv er)); /* Clear struct */
      echoserver.sin_ family = AF_INET; /* Internet/IP */
      echoserver.sin_ addr.s_addr = htonl(INADDR_AN Y); /* Any IP address */
      echoserver.sin_ port = htons(atoi(argv[1])); /* server port */

      /* Bind the socket */
      serverlen = sizeof(echoserv er);
      if (bind(sock, (struct sockaddr *) &echoserver, serverlen) < 0) {
      Die("Failed to bind server socket");
      }

      /* Run until cancelled */
      while (1) {
      /* Receive a message from the client */
      clientlen = sizeof(echoclie nt);
      if ((received = recvfrom(sock, buffer, BUFFSIZE, 0,
      (struct sockaddr *) &echoclient,
      &clientlen)) < 0) {
      Die("Failed to receive message");
      }
      fprintf(stderr,
      "Client connected: %s\n", inet_ntoa(echoc lient.sin_addr) );

      }
      }
      [/code]
      >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>

      Please check my mistake, thanks again for incoming helps,

      Comment

      • rezamirani
        New Member
        • Aug 2007
        • 6

        #4
        I sniffed packets in other machines that connected to the network, packet received but my receive program work bad and can't receive the packets.
        Please checking my receive program "in other side program".

        Best Kings.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by rezamirani
          I sniffed packets in other machines that connected to the network, packet received but my receive program work bad and can't receive the packets.
          Please checking my receive program "in other side program".

          Best Kings.
          Not sure how many of the networking people here are coders, so since you know you are receiving the packet on the destination computer (but not processing it correctly) I'm going to shift this over to the C/C++ forum - hopefully get a few more ideas over there.

          Comment

          • rezamirani
            New Member
            • Aug 2007
            • 6

            #6
            Thanks ,I hope receive any help about this.

            Comment

            Working...