UDP communication over internet problem

Started by
7 comments, last by rOOdolf 20 years ago
First of all apologies if these questions have been asked before .. i''ve tried to go through a reasonable number of back posts looking for these things. I''m a newbie to multiplayer programming, and I''m trying to use UDP for communication. I''ve written most of a quake-like game, for multiplayer use only, but i''m having problems connecting over the internet to a friend running a server on their machine. The client and server are separate in terms of code, but they are build into the same exe, and at the moment the user determines on the command line whether they are running as a client, client and server, or standalone server... and supplies the IP address of the server to be used (or leaves a blank if using a client+server session, where it substitutes hostname for the IP address). Now my big question seems to be to do with the port numbers used. At the moment I have 2 port numbers: 1) client to server port (e.g. 22000) 2) server to client port (e.g. 22001) On a client+server session on the same machine, the client creates a socket to send to the server (port 22000), and listens for replies back on 22001. The server at the same time creates a socket to receive from all clients (22000), and each time a client joins it records the sock_addr thing for the client so it can send back to it during the game. Now what i''m wondering whether is the problem is that it sends back to the client IP address, but on port 22001. When i run the client and server in the same exe the client connects fine and runs. However, when i have given the game to my friends we couldn''t get them to connect over the internet. As i''ve only got one machine here and no LAN, i''m having to work ''blind'' really, so it makes debugging very difficult. Anyway, my first question is does this arrangement of port to server and port to client sound reasonable? I''ve looked at a few games and some use just one port... I can''t seem to create 2 sockets listening at the same port, so i''m guessing in order to get away with just 1 port I''d have to either run as only a client or only a server, or else to use a passthrough that misses out winsock for client server communication on the same machine. I''ve done some reading about NAT and it seems that that might be a possible source of problems .. the first friend i tried trying to connect to my machine (simple DSL connection) was running on an internal network via a router then the internet, so i''m thinking that the NAT thing in the router wasn''t forwarding my replies back to his computer because the port forwarding wasn''t set up. However if i did change over to using 1 port instead of 2, i could send replies back to the extact same IP and port that the messages came to my computer, then this should find itself an entry in the routers temporary NAT table and get sent to the right machine (does that make sense lol?). Anyway, irrelevant of that NAT problem i also tried with a friend with a simple DSL connection too, and he couldn''t connect either. However when i did the test yesterday I wasn''t sure which ports to use and was using 133 and 666, could that have been a problem? I think i''ll have to keep it simple and write a simple console app for sending packets back and forth on different ports to get an idea of what the problems are, but any ideas / tips you guys have would be most appreciated, thanks. :D
Advertisement
you really shouldnt use two ports for a game.
let the server run on a fixed port (22000 in your case) and the client on a random port. this will also pass through a nat router without problems.(if the server is behind the router you will have to forward the port)
the client doesnt need a fixed port because noone ever needs to connect to it.
If it works locally it should work over the internet, are you sure you filled in the right IP address, there are no firewalls, and any routers forward the packets?
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
ahh.. both those bits of information are useful to know.

cody : ahh maybe that is what i''m doing wrong.. basically my server is listening for incoming packets from all clients on the same port.. i.e. i''m not using the ports as 2 way, and that''s what might cause problems with routers :

server : listens to all clients and joins on port A,
sends packets to clients by IP, and always on port B


client : listens port B, sends on port A

but you are suggesting that once joined i listen for each client on it''s own port?

siaon: that is encouraging and what i really wanted to hear.. that the problem is more to do with not having a good line of communication. The first friend i tried it with had a router, which i am suspecting was causing the problem (although there was no way of finding out), the second friend had no router, and no firewall (I think..., although you can never tell, in my experience some firewalls don''t turn off when you switch them off, you have to uninstall them, i.e. zonealarm).

I think i need more friends to be online for testing during work hours lol!!

I guess i''ll try switching to 1 port first, then try using the system siaon suggests. But first i''m just gonna use some basic test UDP ''ping'' type apps to determine whether there actually is a clean line of comunication.

p.s. i don''t think it''s to do with lost packets etc because i''ve tested it thoroughly beforehand with a software ''internet simulator'' layer that simulates varying lag, dropped packets and out of order packets.
"but you are suggesting that once joined i listen for each client on it''s own port?"

i dont know if i understand you right here so i say how it should be:

server: creates a socket with fixed port.

client: creates a socket without specifying a port (0) and connect to the server whos port is known. the server then sees from which port the message came and can answer.
ok :D i''m getting there, thanks cody!!

so the server can find out what port a packet to it was sent on. My only last question (i think ) is, in the client how do i find out which port to listen on?

Or do i not have to, do i just do reads on the same socket that i used to send the join request?
udp is connectionless,
so, there is no ''client'' or ''server''.
this is relevant because the box doing yer router wont automatically forward stuff for udp connections, because no connection is ever really established...
but, if you tell the router explicitally to forward udp packets destined for port whatever to said computer, it should be right.

these dudes seem to have the same idea.
http://mud.5341.com/msg/1314.html


might be wrong, havent read up on this sorta stuff for a while
yup, here''s a good article i found on the NAT issue ...

http://www.intel.com/cd/ids/developer/asmo-na/eng/79524.htm?page=5
right, i''ve implemented it just as cody suggested.. and it turns out I could listen on the same socket i sent the join request to the server on, so no need to specify a particular port.. it presumably just uses the same random port number it chose to start sending to the server from.

It should in theory, get past routers, providing the client is the one on the other side of the router, rather than the server (in which case port forwarding would be needed I''d think).

All that''s left is to test it, once my test subject gets back from work .

This topic is closed to new replies.

Advertisement