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

		}

Dani AI

Generated

Short answer: Internet routers will not forward LAN-style broadcasts across the public network, so a UDP "broadcast" sent on the server's LAN will not reach remote clients. was pointing at that constraint. For a server with a static public IP and an external UDP port forwarded (external 314 -> internal 313), a different approach is needed: unicast or an application-level publish/subscribe.

Practical, low-complexity options for 's setup:

  • Client registration: each client sends a small "hello" packet to the server's public IP:314. The server records the source IP:port seen by recvfrom and thereafter sends datagrams back to those endpoints. Heartbeats are required so NAT mappings stay alive.
  • Persistent connection: have clients open a TCP (or WebSocket) connection to the server; the server pushes updates down that connection (simpler with restrictive NATs).
  • Relay/broker: use a public relay or message broker (app-level multicast) if many clients or reliability/ordering is needed. Multicast across the global Internet is generally unavailable.

Implementation notes and cautions:

  • On the client, using UDP connect() to the server IP sets a default peer and lets recv() drop packets from other sources; alternatively, inspect the source address returned by recvfrom and ignore others. SO_BROADCAST is irrelevant for receiving unicast from the Internet.
  • Ensure the server actually binds to the internal port (313) and the router forwards UDP 314 to it; open OS and network firewalls for UDP.
  • Account for NAT types (symmetric NAT can break UDP hole punching), message size (stay under MTU), and lack of delivery guarantees—add sequence numbers/acks or use TCP if loss is unacceptable. 's hard-coded-IP suggestion works if the IP is stable, but DNS or a small config file is more flexible.

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.