How to Broadcast?
-
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
-
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
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).
-
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).
-
If I use UDP to broadcast, Do I need to use the "setsockopt" function to set the "SO_BROADCAST" flag? Aidman » over and out
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 WindowsWSADATA 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\_
-
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 WindowsWSADATA 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\_
-
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 WindowsWSADATA 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\_
-
Sorry, just one more question... Can I use the same socket to receive broadcast messages? Aidman » over and out
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.
-
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.
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
-
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
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. :)