GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-May-2008, 10:20
curiousguy curiousguy is offline
New Member
 
Join Date: May 2008
Posts: 1
curiousguy is on a distinguished road

UDP Broadcast server


Hi everybody!

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

-> 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.

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.

the server and cllient code is shown below.

Server.cpp
CPP / C++ / C Code:
 
#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
CPP / C++ / C Code:
 
#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();
 
    closesocket(sock);
    WSACleanup();
}
 
 
 

the server and client code run successfully on the same machine.
thx
Last edited by admin II : 14-May-2008 at 17:24. Reason: Changed [CODE] to [CPP]
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BarracudaDrive application server for Linksys NSLU2 BDG Computer Software Forum - Linux 0 24-Mar-2008 14:24
Named virtual host not working Johnnyrotton Apache Web Server Forum 4 04-Sep-2007 20:32
Microsoft SQL Server :: SQL Server message 241, state 1, severity 16 lyuboe MySQL / PHP Forum 0 11-May-2005 04:31
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.