UDP – Broadcast

 

UDP broadcast is a technique that allows sending UDP datagram from a single source to all computers in a LAN. In order to send a UDP datagram addressed to all computers in the local area network it needs to be sent to a special address called the Broadcast address. The broadcast address for a LAN is either the highest address in the local subnetwork or the universal broadcast address: 255.255.255.255. In order to receive broadcast messages the receiver needs to enable SO_BROADCAST option on the socket (see bellow)

UDP Broadcast Sender

UDP Receiver

#include"winsock2.h"

#include<iostream>

#include<conio.h>

 

using namespace std;

 

#define MYPORT 9009    // the port users will be connecting to

 

 

int main()

{

    WSADATA wsaData;

    WSAStartup(MAKEWORD(2,2), &wsaData);

 

    SOCKET sock;

    sock = socket(AF_INET,SOCK_DGRAM,0);

 

    char broadcast = '1';

 

    if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast)) < 0)

    {

        cout<<"Error in setting Broadcast option";

        closesocket(sock);

        return 0;

    }

 

    struct sockaddr_in Recv_addr;  

    struct sockaddr_in Sender_addr;

 

    int len = sizeof(struct sockaddr_in);

 

    char sendMSG[] ="Broadcast message from SLAVE TAG";

 

    char recvbuff[50] = "";

    int recvbufflen = 50;

 

    Recv_addr.sin_family       = AF_INET;        

    Recv_addr.sin_port         = htons(MYPORT);   

//  Recv_addr.sin_addr.s_addr  = INADDR_BROADCAST; // this isq equiv to 255.255.255.255

// better use subnet broadcast (for our subnet is 172.30.255.255)

    Recv_addr.sin_addr.s_addr = inet_addr("172.30.255.255");

 

 

    sendto(sock,sendMSG,strlen(sendMSG)+1,0,(sockaddr *)&Recv_addr,sizeof(Recv_addr));

 

 

    recvfrom(sock,recvbuff,recvbufflen,0,(sockaddr *)&Recv_addr,&len);

 

    cout<<"\n\n\tReceived message from Reader is =>  "<<recvbuff;

 

     cout<<"\n\n\tpress any key to CONT...";

    _getch();

 

    closesocket(sock);

    WSACleanup();

}

 

 

#include"winsock2.h"

#include<iostream>

#include<conio.h>

 

using namespace std;

 

#define MYPORT 9009    // the port users will be connecting to

 

 

int main()

{

    WSADATA wsaData;

    WSAStartup(MAKEWORD(2,2), &wsaData);

 

    SOCKET sock;

    sock = socket(AF_INET,SOCK_DGRAM,0);

 

    char broadcast = '1';

 

//     This option is needed on the socket in order to be able to receive broadcast messages

//   If not set the receiver will not receive broadcast messages in the local network.

    if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast)) < 0)

    {

        cout<<"Error in setting Broadcast option";

        closesocket(sock);

        return 0;

    }

 

    struct sockaddr_in Recv_addr;  

    struct sockaddr_in Sender_addr;

 

    int len = sizeof(struct sockaddr_in);

 

    char recvbuff[50];

    int recvbufflen = 50;

    char sendMSG[]= "Broadcast message from READER";

 

    Recv_addr.sin_family       = AF_INET;        

    Recv_addr.sin_port         = htons(MYPORT);   

    Recv_addr.sin_addr.s_addr  = INADDR_ANY;

 

    if (bind(sock,(sockaddr*)&Recv_addr, sizeof (Recv_addr)) < 0)

    {

        cout<<"Error in BINDING"<<WSAGetLastError();

        _getch();

        closesocket(sock);

        return 0;

    }

 

    recvfrom(sock,recvbuff,recvbufflen,0,(sockaddr *)&Sender_addr,&len);

 

    cout<<"\n\n\tReceived Message is : "<<recvbuff;

    cout<<"\n\n\tPress Any to send message";

    _getch();

 

 

    if(sendto(sock,sendMSG,strlen(sendMSG)+1,0,(sockaddr *)&Sender_addr,sizeof(Sender_addr)) < 0)

    {

        cout<<"Error in Sending."<<WSAGetLastError();

        cout<<"\n\n\t\t Press any key to continue....";

        _getch();

        closesocket(sock);

        return 0;

    }

    else

        cout<<"\n\n\n\tREADER sends the broadcast message Successfully";

 

 

    cout<<"\n\n\tpress any key to CONT...";

    _getch();

 

    closesocket(sock);

    WSACleanup();

}