Followers 0

# UDP-Based Client | Packets don't reach server

## 10 posts in this topic

Hello there,

I've got a problem with my UDP-based gameserver I'm working on.

Everything is running fine when server and clients are running on the same machine and connecting to localhost.

When I have everything running on the same machine but use my public IPv4, packets send by the client are reaching the server, but not the other way around.

When clients and server are running on different machines, none of them are able to recv packets from each other.

I'm using  c++ and winsocks. The port I'm using is open, the IP-address is correct. Server is accepting INADDR_ANY. For testpurposes I'm using the most basic server/client possible in c++, just the

WSAStartup(MAKEWORD(2 ,0), &wsa); SOCKET s = socket(AF_INET,SOCK_DGRAM, IPPROTO_UDP);
sendto(s, sendBuf, sizeof(sendBuf), 0, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));

and a bind for the server. Nothing is returning errors and the server is allowed as acception by the firewall.

I can't seem to find stuff regarding my problem, so I choose to ask this question here. Is every little UPD-program required to do some kind of punchtrough, or am I missing something?

I'd really apprechiate any kind of help, I grew really desperate with this.

Thanks so much for your concern!

Edited by gnomgrol
0

##### Share on other sites

If your machines are on the same network your router has to allow NAT loopback/ for you to be able to use the public IP (not all routers support it and when it is supported it is normally disabled by default), if its not supported you will have to use the LAN ip when the client is on the same network as the server.

The server should always respond on the same socket it recieves from (otherwise firewalls will block the traffic) (a fairly common mistake is to open 2 sockets on the client (one to send and one to recieve, and that just won't work), the server gets the correct port and ip to send to from the recvfrom call.

Edited by SimonForsman
1

##### Share on other sites

Another possibility is that you are not resolving the IP correctly. Have you tried using connect instead and send? This way you may check if the server name resolution is correct.

Here is an example (this example uses localhost - 127.0.0.1 - and port 27015, you must, of course, change it for your server IP and port):

http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx

For testing purposes you can also use nc (netcat), just config it to use udp instead of the default tcp.

0

##### Share on other sites

Right -- where does the value of addr come from? Are you properly using htons() for the port in there? Are you setting the sin_af field? Where do you get the address value from?

Also, you might as well pass in 2,2 for the WinSock version number -- it's been the current version for 15 years or so.

0

##### Share on other sites

The server should always respond on the same socket it recieves from (otherwise firewalls will block the traffic) (a fairly common mistake is to open 2 sockets on the client (one to send and one to recieve, and that just won't work), the server gets the correct port and ip to send to from the recvfrom call.

That was actually why it was working only one way when using my public IP, thanks!

Unfortunately, it still does not work with client and server not in the same network, I'm still not getting any packets.

Another possibility is that you are not resolving the IP correctly.

Are you properly using htons() for the port in there?

Both of those are fine.

Are you setting the sin_af field? Where do you get the address value from?

What sin_af field are you talking about? The address is from the recvfrom (for the server) and set like this in the client:

memset(&addr, 0, sizeof(SOCKADDR_IN));

Edited by gnomgrol
0

##### Share on other sites

Are you checking the return of the inet_addr? It may be returning error.

1

##### Share on other sites

Okey, I fixed it! Your last tip was pointing me in the right direction, one needs to call bind and connect in the client, and then use send. Thanks a bunch!

EDIT: Well, not quite. Bind does not seem to have much effect in the client, calling connect is the thing allowing packets to be send and recived. But, when I use it like this in different networks, it drops like 70% of all packets (I know UDP is supposed to work like this, but THAT many?), and after a few seconds, it simply dies. Since this only happens  when using this on different PCs, i can't quite debug the client that way :/

Do you have any idea what could cause this?

Edited by gnomgrol
0

##### Share on other sites

Check your send and receive buffer sizes - if you don't explicitly set them they can be quite small (something like 4K bytes I think). Any packets that overflow these buffers when sending or receiving are just dropped. I think the funtion is called setsockopt().

0

##### Share on other sites

Are you timing the sends (for instance, 1 per second) or sending packets in an infinite loop? If you are not timing you will surelly send way more packages than your network can handle and you will drop a load of them. Program dieing is likely some problem with your buffers.

Edited by KnolanCross
0

##### Share on other sites

connect() shouldn't matter at all for UDP as long as you use sendto(). It just allows you to use send() instead of sendto(). Note that on the server, you cannot call connect() at all, because you will need to send datagrams to all different clients on the same socket.

What does WireShark say on the sending and receiving machines when it works, and when it doesn't work?

0

##### Share on other sites

Kinda strange. Using connect and send in the client, while using recv instead of recvfrom in the server fixed it. I can't really see why, but still, thanks for your help!

0

## Create an account

Register a new account