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

		}

Recommended Answers

All 3 Replies

Broadcast to the Internet?
Not possible.
Even I would receive your packets.
Every router prevents that.

The Static IP is not known to every one, and the machine that has static IP assigned by ISP is broadcasting(my program above), so how can i connect to it from internet, is there any source code example that could give me go ahead. we have also implemented port forwarding if it could help.

Thanks

If your server's IP-address is truly static, you could hard-code the server IP address in to the client program. Since it's the only address(is it?) the client is supposed to receive UDP-datagrams from, in this case it is perhaps acceptable. Usually this is not the way to do it though.

There are loads of socket programming examples on the internet, try keywords such as [socket programming UDP client server C]

I suggest you take a look at some socket documentation, especially functions like gethostbyname, bind vs connect and htons/ntohs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.