Sign in to follow this  
curiousGUY

Broadcast UDP client server

Recommended Posts

Hi all! i build a simple UDP client server, inwhich the client sends the message and the server receives the message. the code is here. //////////////////SERVER.cpp////////// #include <winsock.h> #include<iostream> #include<conio.h> using namespace std; int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); SOCKET RecvSocket; RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sockaddr_in RecvAddr; sockaddr_in SenderAddr; int Port = 2345; char RecvBuf[100]; int BufLen = 100; int SenderAddrSize = sizeof(SenderAddr); RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = INADDR_ANY; bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr)); recvfrom(RecvSocket,RecvBuf, BufLen,0,(SOCKADDR *)&SenderAddr, &SenderAddrSize); cout<<"\n\n\tReceived Message from client:\t"<<RecvBuf; closesocket(RecvSocket); WSACleanup(); } /////////////////////CLIENT.cpp////////////////// #include <winsock.h> #include<iostream> #include<conio.h> using namespace std; int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); SOCKET SendSocket; SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sockaddr_in RecvAddr; int Port = 2345; char ip[] = "127.0.0.1"; char SendBuf[] = "hello! Message from client."; int len = sizeof(struct sockaddr_in); RecvAddr.sin_family = AF_INET; RecvAddr.sin_port = htons(Port); RecvAddr.sin_addr.s_addr = inet_addr(ip); sockaddr_in SenderAddr; sendto(SendSocket,SendBuf,strlen(SendBuf)+1,0,(SOCKADDR *) &RecvAddr, sizeof(RecvAddr)) _getch(); closesocket(SendSocket); WSACleanup(); } //////////////////// now anyone can tell me how can i modify or add the code that both client and server can send and receive the broadcast messages. THX for cooperation in advance

Share this post


Link to post
Share on other sites
Before binding your socket, set the socket options to allow broadcasting.


// set socket option to allow for broadcasting
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
if (SOCKET_ERROR == setsockopt(m_hSocket, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen))
{
StopNetwork();
return -1;
}




Hope that helps.

Share this post


Link to post
Share on other sites
Do you really want broadcast (i.e. all hosts on the same physical cable) or maybe multicast?

Broadcast will only work for LAN, and it's maybe not the most efficient thing to do, since every single host on the cable must process every packet. If that's what you wanted, forget my question :)

Share this post


Link to post
Share on other sites

THX Amadeus and samoth for ur cooperation

i am new in network programming so thats why i am not good in it.

Amadeus u suggested me to add few lines of code (that u send me) before binding the socket.

i am binding the socket just on the server side by BIND() function but not on the client side.

basically i try to build such server and client inwhich the server send the broadcast message so the client receive it and again this time the client sends the broadcast message as a response.

can u please tell me how i can do this.

one more thing, is it possible the server and the client both run on the same PC.

THX for ur help.

Share this post


Link to post
Share on other sites
THX Amadeus for ur cooperation

i build a simple UDP client server inwhich (1 server and 1 client)

-> the client sends the broadcast request and server receives it.

-> after receiving the broadcast request the server sends the response back to the client as broadcast response.

the server and client code run successfully on the same machine.
the code is here.

Server.cpp

#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 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();
}



CLIENT.cpp

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



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();




Now i try to convert this code into which (1 server and 2 clients)

-> the server first sends the broadcast request . Both Client receives it.

-> after receiving the request, both clients sends back their broadcast response to the server.

is anybody know how to do this or any idea?

THX for in advance


Share this post


Link to post
Share on other sites
Quote:
Original post by curiousGUY
Now i try to convert this code into which (1 server and 2 clients)

-> the server first sends the broadcast request . Both Client receives it.

-> after receiving the request, both clients sends back their broadcast response to the server.

is anybody know how to do this or any idea?

THX for in advance
That code will only work when the client and server are on the same network, it won't work over the Internet for obvious reasons.

As for implementing it, the server just needs to send a packet to INADDR_ANY, and the clients just need to wait for it to arrive, then send a packet to the address they got from recvfrom().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this