Archived

This topic is now archived and is closed to further replies.

Evil Bill

WinSock and NATs

Recommended Posts

Yup, its me again... I''ve finally got everything figured out, and it works great. The only problem is with NATs (Network Address Translators). I am on a home network, with 3 computers. Fortunately, i''m the computer with the net connection. The problem comes when i try to use my chat program (prrrfftt! if you can call it a chat program) on my sisters computer. We use windows internet connection sharing, so she can connect to the internet through my computer. The only problem with this, is that my program can only connect to the network, it isn''t aware of the interent connection sharing. So, on to the question: Is there any way to get the IP of a computer on the other side of a NAT? E.g. can i get my sisters internet IP? At the moment i use gethostbyname(NULL) to get the IP addresses of the current computer. How do most games / apps handle this? Thanks for all the help guys, Steve Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
Are youi trying to originate your connection from inside or outside of the NAT?

Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349

Share this post


Link to post
Share on other sites
Either.
At the moment i''m trying to connect to my friends computer (which is behind a router) from my internet connection.

Lets just assume i have no NAT on my side just now (because its not in use at the moment).

Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
The only way to connect to a machine that is behind a NAT is to have them configure it for forwarding. The NAT device will then forward all requests it gets to for a particualr port to the specified machine. The machine can then handle it.

When you connect though, you specify the NAT device address, not the internal machine address. The internal machine is using an illegal addres that internet routers will not forward.

are you using TCP or UDP?

Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349

Share this post


Link to post
Share on other sites
if your friends computer is behind a NAT then the only way you can connect to them is if they have that NAT set up to route your particular application port to their machine. otherwise you are out of luck.

the way chat programs work is that everyone is aware of the central server''s IP address. when you log onto AIM or whatever, you make a listen connection out to the main server. when you send a message, you send to the server and then the server sends your message down your friends listen connection. i imagine that every message you send has some sort of id number associated with it that the server uses to send a message out the correct pipe.

NATs are used as dumb firewalls b/c they by default don''t route any incoming data to any of the internal computers unless they are speciffically told to route a certain port to a certain machine. you may be able to config the NAT to broadcast all incoming data on a specific port to all the machines inside, but i''m not sure about that. my NAT box doesn''t allow that.

basically you need a central server that is not behind a NAT to control everything.

-me

Share this post


Link to post
Share on other sites
You can initiate a connection from inside a NAT device if your going to a legal address. UDP sometimes doesn''t work this way but TCP always does. Game servers have legal addresses so they can be connected to.

It''s trying to get into a NAT network that is the hard part. Getting out is easy.

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Bill
How do other games do it?



incedentally games do it the same as chat progs w/ a central server. that''s why you can''t host a game server unless you are either outside of a NAT or have opened all the proper ports on the router to go to your machine.

trying to set up your NAT to route ALL the proper game ports to your machine can be a nightmare.

-me

Share this post


Link to post
Share on other sites
Jeez, my inbox is filling up with all these replies!

quote:

It''s trying to get into a NAT network that is the hard part. Getting out is easy.


I set it up so my friend (behind the router) was the client, trying to connect to me, and that didn''t work? All ive been able to test at the moment is across my home network. I''m hoping to test it over modem to another friend soonish.

Cheers, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
The person who has the "server" (the one your trying to connect to) must have a legal internet address, not behind a NAT device.

Your friend is probably behind a NAT router, like the famous Linksys router. He will have to plug his machine directly in to the modem (whether dsl or cable, doesn''t matter). Then try it.

Share this post


Link to post
Share on other sites
quote:

The person who has the "server" (the one your trying to connect to) must have a legal internet address, not behind a NAT device.


I''m the server. I call listen(). I''m plugged directly into the modem.
I''m just going to see what the error message is, i''ll post back in 5 mins...

Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
Ok then. Are you imbeddding the "client" address in your packet or are you getting it from the socket description? You must do a recvfrom and read the address out of the packet information.

Share this post


Link to post
Share on other sites
The error is: WSAETIMEDOUT.

Ah... I''m just getting it from the socket description...
Right, i''ll give that a shot. I don''t need to connect the socket, right? Just call recvfrom()?

Cheers, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
Never thought of that: Port 6969
I know theres certain ones your supposed to use, but this is just a test.
What ones should you use? I read that games like quake use 27000+.

(I tried recvfrom() anyway BTW, and it just didn''t fill in the descriptor i passed (yep, i did set ''len'' correctly - i missed that a while ago. Spent ages trying to fix it )

Steve

Share this post


Link to post
Share on other sites
if you use ICS, its quite possible that its treating even your pc as the one behind the NAT. make sure you use your internet address (ie external) not your lan address (internal). since you have 2 nics your app may be listenning on the wrong ip address. i assume 2 nics since thats how most nats get setup. 1 for the internet and one for th lan connected to the hub/switch. you may wish to use INADDR_ANY as the ip address (its a constant) which tells winsock to listen on ALL ip addresses that the machine has (local and external).

basiclly you never have to deal with NATs specially in your code beyond allowing the user to choose the ip and port they wish to listen to (and by default possible use INADDR_ANY). your method (ie gethostname(NULL)) is most likly picking the nic which is for the lan.

use gethostname(arrayForHostname, SIZE_OF_ARRAY) to get the hostname for the pc. then gethostbyname() and go through the entire list of ips returned (there can be more then one).

warning the hostname returned may not be the internet hostname, please check the docs for more info.

Share this post


Link to post
Share on other sites
Yup, ive got 2 addresses.
I already use INADDR_ANY.
Setting it to my real IP doesn''t help
I use gethostbyname() to build a list of IPs to display to the user so they can say "Yeah, my IP is a.b.c.d, connect in 5 mins" or whatever.

Cheers, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites