1

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 );
1
  • Most likely, the problem is a firewall at the server side. Does UDP traffic using netcat work fine? If not, first fix the firewalls (on a home router, you have to configure port forwarding at the server side, or even employ an elaborate NAT traversal scheme in your application).
    – phihag
    Oct 26, 2013 at 16:28

1 Answer 1

2

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.