Hi All,
I am a software developer C/C++, not expert in network programming but did a little bit. I work as s free lancer and have been tasked to implement UDP server (Broadcaster) over internet (Server machine has Static IP assisgned by ISP so reacheable from any part of world, with port forwarding local:313 for outside world:314).
The Server needs to broadcast data on start up till machine is shut down.
The Clients have to receive from Static IP i.e from Server IP the udp packets.
The Traffic would be simplex(one way only) from server to client even if there are no clients. The Server has to just broadcast from start to end.
I wanted to know that given static IP(internet) of server and port forwarded what client code i need to write that enable me to receive from server UDP packets.
This is the client code that received broadcast UDP packets from LAN.
(note)recvfrom points to location the packet has come from but i think
i have to receive only from server(Static IP) so where i should put static IP of server to receive it's broadcast from internet
///////////code////////////////////////////////
WSADATA wsaData;
char buffer[30];
if ( WSAStartup( MAKEWORD(1,1), &wsaData)!=0 )
{
AfxMessageBox("Error initializing Port");
}
else
{
sock= socket(AF_INET,SOCK_DGRAM,0);
len= sizeof(struct sockaddr_in);
char broadcast='1';
if( setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast) )<0 )
{
AfxMessageBox("Error Setting Broadcst option");
closesocket(sock);
exit(1);
}
me.sin_family = AF_INET;
me.sin_port =htons(port);
me.sin_addr.s_addr=INADDR_ANY;
if( bind(sock, (sockaddr*) & me, sizeof (me) ) < 0 )
{
AfxMessageBox("Error Binding");
closesocket(sock); WSACleanup();
}
printf("This is Client");
while(1)
{
int count=recvfrom(sock,(char *)&Data_Frame,sizeof(Data_Frame),0,(sockaddr *) & me,&len );
if ( count ==sizeof(Data_Frame))
printf("Cost=%.2f,Charges=%.2f",Data_Frame.Cost,Data_Frame.Charges);
}
}