Finding the connected client's IP
I am programming a client/server for updating game files, similar to the kind of file updater you see when you start any of todays MMORPGs. The server is a console, strictly text display.
What I need is a method of finding the IP of the connected client so it can be displayed/logged. I have tried a couple methods, but they all return the local IP, which is usually fine except in the case where someone is using a NAT. In that case they send their local network IP (192.168.1.100) instead of their global IP. Is there any way for my server to retrieve a connected clients IP address, and convert it to a format that I can display/log? Im using c++/win32/DirectPlay. Thanks in advance!
-Noods
December 09, 2003 09:08 AM
CComPtr< IDirectPlay8Address > l_pPeerAddr = NULL;WCHAR l_wcBuffer[ 128 ];char l_cName[128];DWORD l_dwSize = 128;DWORD l_dwPort;DWORD l_dwType;long l_lAddr;IDirectPlay8Server::GetClientAddress( dpnid, &l_pPeerAddr, 0 );l_dwSize = sizeof( l_wcBuffer );l_pPeerAddr->GetComponentByName( DPNA_KEY_HOSTNAME, l_wcBuffer, &l_dwSize, &l_dwType );ConvertWideToAnsi( l_cName, l_wcBuffer );l_lAddr = ::inet_addr( l_cName );
This worked to perfection, however it still gave the local IP (192.168.1.1 from the router) rather then the global IP of the used connection. Is there no way to get the global IP if a user is behind a NAT?
December 10, 2003 07:56 AM
Are by any chance both the server and the client plugged on the *same* router?
The client can''t help but report its local IP. That''s the only one it knows about. The server can report the IP of whatever it sees when it accepts() an incoming connection. If the client is behind a NAT from the server, these values will likely be different and theres not too much you can do about it.
December 10, 2003 03:06 PM
The server should see the internet IP of all its clients regardless if they are behind a NAT or not. A machine can''t see its *own* internet IP if it''s behind a router. In this case, either the DirectPlay API is broken or the DPNID used in the GetClientAddress() call is that of the local machine.
Again, you are correct. The client and server are behind the same router. I assume if I put one on another computer behind a different router it will detect the correct address? If this works, Im going to knight Mr AP
![](wink.gif)
December 11, 2003 08:38 AM
The client and server are behind the same router.
The only way you can get the true ''internet'' IP for your server is to bounce some messages to some other server across the globe (a la GameSpy) or to negotiate a ''fixed IP'' address with your provider. A fixed IP will make your computer visible to the outside world, but make it vulnerable to attacks; routers and NATs protect you by mangling port numbers and closing unused ports. You can use a service like http://www.myip.org/ that will name your IP for you using some DNS sub-server or use a bouncing server like http://www.whatismyip.com/ or http://www.myip.com/ that will get you back your true internet IP. As for the port, you will have to program your router to open up the port number you plan to use in your game server so that port is available on the WAN side of your router.
The only way you can get the true ''internet'' IP for your server is to bounce some messages to some other server across the globe (a la GameSpy) or to negotiate a ''fixed IP'' address with your provider. A fixed IP will make your computer visible to the outside world, but make it vulnerable to attacks; routers and NATs protect you by mangling port numbers and closing unused ports. You can use a service like http://www.myip.org/ that will name your IP for you using some DNS sub-server or use a bouncing server like http://www.whatismyip.com/ or http://www.myip.com/ that will get you back your true internet IP. As for the port, you will have to program your router to open up the port number you plan to use in your game server so that port is available on the WAN side of your router.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement