Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Broadcast?

How to Broadcast?

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintutorialquestionlearning
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aidman
    wrote on last edited by
    #1

    Hi all, :) How to broadcast a message within a LAN using Winsock? Meaning how to send a message to every machine in a local network without knowing their IP addresses, of course you do know the port you’re using. Like when games find all servers in a network without knowing they are there. Aidman » over and out

    J 1 Reply Last reply
    0
    • A Aidman

      Hi all, :) How to broadcast a message within a LAN using Winsock? Meaning how to send a message to every machine in a local network without knowing their IP addresses, of course you do know the port you’re using. Like when games find all servers in a network without knowing they are there. Aidman » over and out

      J Offline
      J Offline
      JohnnyG
      wrote on last edited by
      #2

      You need to read up on Winsock. There are some articles here. As far as discovering other servers for a game environment etc. It depends on how each server is set up. For example, if the server is always listening for a connection on a particular port, it may be as simple as broadcasting a message to all IPs on that port and see which ones respond. Of course, there should already be an established protocol on how a game running on a client computer would do this. But, sending a broadcast message is a connectionless datagram protocol such as UDP. Usually, you would send the message to IP broadcast address of 255.255.255.255 or your IP's subnet as xxx.xxx.xxx.255. If you read up on how to send a UDP network message, its simply a matter of putting the broadcast address as your destination address (the PC you're sending the message to).

      A 1 Reply Last reply
      0
      • J JohnnyG

        You need to read up on Winsock. There are some articles here. As far as discovering other servers for a game environment etc. It depends on how each server is set up. For example, if the server is always listening for a connection on a particular port, it may be as simple as broadcasting a message to all IPs on that port and see which ones respond. Of course, there should already be an established protocol on how a game running on a client computer would do this. But, sending a broadcast message is a connectionless datagram protocol such as UDP. Usually, you would send the message to IP broadcast address of 255.255.255.255 or your IP's subnet as xxx.xxx.xxx.255. If you read up on how to send a UDP network message, its simply a matter of putting the broadcast address as your destination address (the PC you're sending the message to).

        A Offline
        A Offline
        Aidman
        wrote on last edited by
        #3

        If I use UDP to broadcast, Do I need to use the "setsockopt" function to set the "SO_BROADCAST" flag? Aidman » over and out

        J 1 Reply Last reply
        0
        • A Aidman

          If I use UDP to broadcast, Do I need to use the "setsockopt" function to set the "SO_BROADCAST" flag? Aidman » over and out

          J Offline
          J Offline
          JohnnyG
          wrote on last edited by
          #4

          Yes, you do. Here is a sample program I wrote to perform broadcast. It works on both Linux, Posix (for our embedded system), and Windows. For Windows, you need to #define WINMACHINE:

          // TEST PROGRAM TO BROADCAST ON UDP SOCKET. Should work on both POSIX and Windows
          // to compile/run in Windows, define WINMACHINE .e.g #define WINMACHIN
          #include <stdio.h>
          #include <stdlib.h>
          #include <time.h>
          #include <string.h> /* strlen() */

          // platform specific includes
          #ifdef WINMACHINE // for Windows

          #include <windows.h>
          #include <winsock.h>
          #include <wincon.h>
          #include <conio.h>

          #else // for unix/POSIX

          #include <machine/mpc823.h>
          #include <sys/types.h>
          #include <sys/socket.h> /* sockaddr_in */
          #include <sys/errno.h> // for global errno
          #include <netinet/in.h> /* AF_INET, etc. */
          #include <pthread.h> /* pthread_create() */
          #include <netinet/udp.h>

          //#include <curses.h>

          #endif

          #define TXOUTBUFSZ 10000 // Output buffer size
          #define RADAR_PORT 44068 // IP/UDP Port number for trans/rcv

          // platform specific data declarations and/or definitions
          #ifdef WINMACHINE // for Windows

          WSADATA wsaData;
          SOCKET sock;
          SOCKADDR_IN dst_addr, src_addr;
          int dst_addr_len = sizeof(dst_addr);

          #else // for unix/POSIX

          int sock;
          struct sockaddr_in dst_addr, src_addr;
          #endif

          // non-platform specific declarations
          int rc;

          int main()
          {
          char sampleMsg[132];
          int optval = 1;
          int optlen = sizeof(int);

          dst\_addr.sin\_family = AF\_INET;
          dst\_addr.sin\_port = htons(RADAR\_PORT);
          dst\_addr.sin\_addr.s\_addr = INADDR\_BROADCAST;  // inet\_addr("255.255.255.255"); //htonl(INADDR\_ANY);
          
          strcpy(sampleMsg, "This is a test sample msg from the box. ");
          

          #ifdef WINMACHINE // for Windows
          // Start WinSock in version 2.0 of the protocol
          rc = WSAStartup (MAKEWORD (1, 0), &wsaData); // was MAKEWORD(1,1)
          if(rc != 0)
          {
          printf("Error starting WinSock\n");
          goto error;
          }

          // Create a UDP socket for receiving datagrams
          sock = socket(AF\_INET,SOCK\_DGRAM, 0);
          if(sock == INVALID\_SOCKET)
          {
          	printf("Error creating receive socket\\n");
          	goto error;
          }
          
          // Set up the socket to do broadcast messages...this may not be needed
          rc = setsockopt(sock, SOL\_
          
          A 2 Replies Last reply
          0
          • J JohnnyG

            Yes, you do. Here is a sample program I wrote to perform broadcast. It works on both Linux, Posix (for our embedded system), and Windows. For Windows, you need to #define WINMACHINE:

            // TEST PROGRAM TO BROADCAST ON UDP SOCKET. Should work on both POSIX and Windows
            // to compile/run in Windows, define WINMACHINE .e.g #define WINMACHIN
            #include <stdio.h>
            #include <stdlib.h>
            #include <time.h>
            #include <string.h> /* strlen() */

            // platform specific includes
            #ifdef WINMACHINE // for Windows

            #include <windows.h>
            #include <winsock.h>
            #include <wincon.h>
            #include <conio.h>

            #else // for unix/POSIX

            #include <machine/mpc823.h>
            #include <sys/types.h>
            #include <sys/socket.h> /* sockaddr_in */
            #include <sys/errno.h> // for global errno
            #include <netinet/in.h> /* AF_INET, etc. */
            #include <pthread.h> /* pthread_create() */
            #include <netinet/udp.h>

            //#include <curses.h>

            #endif

            #define TXOUTBUFSZ 10000 // Output buffer size
            #define RADAR_PORT 44068 // IP/UDP Port number for trans/rcv

            // platform specific data declarations and/or definitions
            #ifdef WINMACHINE // for Windows

            WSADATA wsaData;
            SOCKET sock;
            SOCKADDR_IN dst_addr, src_addr;
            int dst_addr_len = sizeof(dst_addr);

            #else // for unix/POSIX

            int sock;
            struct sockaddr_in dst_addr, src_addr;
            #endif

            // non-platform specific declarations
            int rc;

            int main()
            {
            char sampleMsg[132];
            int optval = 1;
            int optlen = sizeof(int);

            dst\_addr.sin\_family = AF\_INET;
            dst\_addr.sin\_port = htons(RADAR\_PORT);
            dst\_addr.sin\_addr.s\_addr = INADDR\_BROADCAST;  // inet\_addr("255.255.255.255"); //htonl(INADDR\_ANY);
            
            strcpy(sampleMsg, "This is a test sample msg from the box. ");
            

            #ifdef WINMACHINE // for Windows
            // Start WinSock in version 2.0 of the protocol
            rc = WSAStartup (MAKEWORD (1, 0), &wsaData); // was MAKEWORD(1,1)
            if(rc != 0)
            {
            printf("Error starting WinSock\n");
            goto error;
            }

            // Create a UDP socket for receiving datagrams
            sock = socket(AF\_INET,SOCK\_DGRAM, 0);
            if(sock == INVALID\_SOCKET)
            {
            	printf("Error creating receive socket\\n");
            	goto error;
            }
            
            // Set up the socket to do broadcast messages...this may not be needed
            rc = setsockopt(sock, SOL\_
            
            A Offline
            A Offline
            Aidman
            wrote on last edited by
            #5

            GREAT THANKS!!! really cool code =) Aidman » over and out

            1 Reply Last reply
            0
            • J JohnnyG

              Yes, you do. Here is a sample program I wrote to perform broadcast. It works on both Linux, Posix (for our embedded system), and Windows. For Windows, you need to #define WINMACHINE:

              // TEST PROGRAM TO BROADCAST ON UDP SOCKET. Should work on both POSIX and Windows
              // to compile/run in Windows, define WINMACHINE .e.g #define WINMACHIN
              #include <stdio.h>
              #include <stdlib.h>
              #include <time.h>
              #include <string.h> /* strlen() */

              // platform specific includes
              #ifdef WINMACHINE // for Windows

              #include <windows.h>
              #include <winsock.h>
              #include <wincon.h>
              #include <conio.h>

              #else // for unix/POSIX

              #include <machine/mpc823.h>
              #include <sys/types.h>
              #include <sys/socket.h> /* sockaddr_in */
              #include <sys/errno.h> // for global errno
              #include <netinet/in.h> /* AF_INET, etc. */
              #include <pthread.h> /* pthread_create() */
              #include <netinet/udp.h>

              //#include <curses.h>

              #endif

              #define TXOUTBUFSZ 10000 // Output buffer size
              #define RADAR_PORT 44068 // IP/UDP Port number for trans/rcv

              // platform specific data declarations and/or definitions
              #ifdef WINMACHINE // for Windows

              WSADATA wsaData;
              SOCKET sock;
              SOCKADDR_IN dst_addr, src_addr;
              int dst_addr_len = sizeof(dst_addr);

              #else // for unix/POSIX

              int sock;
              struct sockaddr_in dst_addr, src_addr;
              #endif

              // non-platform specific declarations
              int rc;

              int main()
              {
              char sampleMsg[132];
              int optval = 1;
              int optlen = sizeof(int);

              dst\_addr.sin\_family = AF\_INET;
              dst\_addr.sin\_port = htons(RADAR\_PORT);
              dst\_addr.sin\_addr.s\_addr = INADDR\_BROADCAST;  // inet\_addr("255.255.255.255"); //htonl(INADDR\_ANY);
              
              strcpy(sampleMsg, "This is a test sample msg from the box. ");
              

              #ifdef WINMACHINE // for Windows
              // Start WinSock in version 2.0 of the protocol
              rc = WSAStartup (MAKEWORD (1, 0), &wsaData); // was MAKEWORD(1,1)
              if(rc != 0)
              {
              printf("Error starting WinSock\n");
              goto error;
              }

              // Create a UDP socket for receiving datagrams
              sock = socket(AF\_INET,SOCK\_DGRAM, 0);
              if(sock == INVALID\_SOCKET)
              {
              	printf("Error creating receive socket\\n");
              	goto error;
              }
              
              // Set up the socket to do broadcast messages...this may not be needed
              rc = setsockopt(sock, SOL\_
              
              A Offline
              A Offline
              Aidman
              wrote on last edited by
              #6

              Sorry, just one more question... Can I use the same socket to receive broadcast messages? Aidman » over and out

              J 1 Reply Last reply
              0
              • A Aidman

                Sorry, just one more question... Can I use the same socket to receive broadcast messages? Aidman » over and out

                J Offline
                J Offline
                JohnnyG
                wrote on last edited by
                #7

                Yep. Set up a in_addr structure and a receive buffer to receive data from the specified port and set the IP address you're receiving from to INADDR_ANY. When you do a receive you have to do a bind() first (during the initialization). Should be plenty of examples around.

                A 1 Reply Last reply
                0
                • J JohnnyG

                  Yep. Set up a in_addr structure and a receive buffer to receive data from the specified port and set the IP address you're receiving from to INADDR_ANY. When you do a receive you have to do a bind() first (during the initialization). Should be plenty of examples around.

                  A Offline
                  A Offline
                  Aidman
                  wrote on last edited by
                  #8

                  Ok thanks very much! I am very sorry, could I ask a last another question? :-O How do you create a no blocking/waiting socket for recveiving? So that if there is no incoming data available for the socket at the receiving function "recvfrom" it would continue instead of waiting for incoming data. Can you make the socket nonblocking or must you make the recievning function nonblocking? Is it possible to do this without having multithreads or network events? Aidman » over and out

                  J 1 Reply Last reply
                  0
                  • A Aidman

                    Ok thanks very much! I am very sorry, could I ask a last another question? :-O How do you create a no blocking/waiting socket for recveiving? So that if there is no incoming data available for the socket at the receiving function "recvfrom" it would continue instead of waiting for incoming data. Can you make the socket nonblocking or must you make the recievning function nonblocking? Is it possible to do this without having multithreads or network events? Aidman » over and out

                    J Offline
                    J Offline
                    JohnnyG
                    wrote on last edited by
                    #9

                    The short answer: Yes, you can make a socket blocking or non-blocking. Better answer: Get this book, "Network Programming for Microsoft Windows", Second Edition, Anthony Jones & Jim Ohlund. I got it off of half.com for $15 or $20 bucks. Well worth it especially since it retails for $59.00 Or, read up more on Winsock and sockets. :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups