Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


UDP-Based Client | Packets don't reach server


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 gnomgrol   Members   -  Reputation: 625

Like
0Likes
Like

Posted 24 June 2013 - 11:19 AM

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, 24 June 2013 - 11:26 AM.


Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 6318

Like
1Likes
Like

Posted 24 June 2013 - 02:42 PM

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, 24 June 2013 - 02:46 PM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#3 KnolanCross   Members   -  Reputation: 1364

Like
0Likes
Like

Posted 24 June 2013 - 03:40 PM

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.


Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


#4 hplus0603   Moderators   -  Reputation: 5723

Like
0Likes
Like

Posted 24 June 2013 - 03:46 PM

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.


enum Bool { True, False, FileNotFound };

#5 gnomgrol   Members   -  Reputation: 625

Like
0Likes
Like

Posted 25 June 2013 - 02:58 AM

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)); 
addr.sin_family = AF_INET;
addr.sin_port = htons(1337);
addr.sin_addr.s_addr = inet_addr("192.52.25.175");

Edited by gnomgrol, 25 June 2013 - 03:05 AM.


#6 KnolanCross   Members   -  Reputation: 1364

Like
1Likes
Like

Posted 25 June 2013 - 10:46 AM

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

 

I checked your code against mine, the only difference I found is that in my client I use inet_aton instead to start the address (and I use connect/receive/send instead of receive_from/sendto).


Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


#7 gnomgrol   Members   -  Reputation: 625

Like
0Likes
Like

Posted 25 June 2013 - 12:40 PM

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, 25 June 2013 - 01:42 PM.


#8 PeterStock   Members   -  Reputation: 396

Like
0Likes
Like

Posted 25 June 2013 - 02:54 PM

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().



#9 KnolanCross   Members   -  Reputation: 1364

Like
0Likes
Like

Posted 25 June 2013 - 04:17 PM

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, 25 June 2013 - 04:26 PM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


#10 hplus0603   Moderators   -  Reputation: 5723

Like
0Likes
Like

Posted 25 June 2013 - 06:58 PM

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?


enum Bool { True, False, FileNotFound };

#11 gnomgrol   Members   -  Reputation: 625

Like
0Likes
Like

Posted 26 June 2013 - 05:49 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS