Frage

Hi I started programming UDP sockets recently and I've been using them for all kinds of stuff on a local network. but I cannot seem to find anything about how to send a UDP packet outside my lovely firewall.

Here's the code i use for binding UDP sockets

int handle = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( (unsigned short) /* port */);
bind( handle, (const sockaddr*) &address, sizeof(sockaddr_in) )

and here's the code i use for sending a packet

sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl( /*local network IP Address*/ );
address.sin_port = htons( (unsigned short) /* port */ );

sendto( handle, (const char*)/* packet data */, size, 0, (sockaddr*)&address, sizeof(sockaddr_in) );

And finally here's the code I use for receiving packets

sockaddr_in from;
socklen_t fromLength = sizeof( from );

int received_bytes = recvfrom( handle, (char*)data, size, 0, (sockaddr*)&from, &fromLength );
War es hilfreich?

Lösung

Well, since UDP is a "not connected" way of sending data you have to add rules to your router's firewall so that it routes the port (to the computer that is supposed to receive the data).

The problem doesn't come from the code, but from the network infrastructure.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top