Retrieving Your IP Address

Started by
11 comments, last by sordid 18 years, 9 months ago
I was trying to figure out why my friend couldn't connect to my game's server on my computer so I looked it up in the forum FAQ, and found this
Quote: Q18) How do I get the IP address of the machine I'm running on? Typically, it's a bad idea to get your own IP address, because it's not certain that the address is visible to people outside your router. Also, many machines have more than one IP address (each interface has one in a machine). Last, not all ISPs or routers have DNS/reverse DNS set up such that gethostname() followed by gethostbyname() will give you the right answer. To use command-line tools to list your addresses on Windows, use "ipconfig". On Linux, use "ifconfig". On BSD, use "ifconfig" but you probably have to be root. To programmatically determine the CORRECT address to use, you have to bounce a query off a server that's available on the public internet, and look at what address it returns back. This will work correctly for situations where you're behind NAT (routers/firewalls). If you want to know the list of all interface addresses, say for selectively binding to only some interface, then you need to use OS-specific system calls; i e you need to do whatever ipconfig/ifconfig does, which involves the network device API on Windows, and walking /proc on Linux.
What I got from that is that I need to send a request for my IP to some server somewhere and record its response. Can anyone tell me how to do this, or show me a tutorial on the subject. Thanks.
My Current Project Angels 22 (4E5)
Advertisement
If you're being a gateway, and only have an internal IP address assigned to your computer.. and want to know what IP address your traffic is sent out from (and expects to receive responses to)

www.whatismyip.com

Unless you really want to get the code itself to do this.. then I completely misinterpreted the required end result and I'll go back to watching t.v.

The biggest issue that I see with trying to code in a method to retrieve your publically visible IP address is that your NAT daemon will molest the packets to remove that IP address.. and bungling with the daemon's code could introduce compatibility problems with other active interfaces.
So then how can I allow my friend to join my server? How do other games like Quake and Unreal do this? My game works over a LAN, but I would really like to b able to run it ovewr the internet.
My Current Project Angels 22 (4E5)
Most likely you'll need to set up a port mapping in your gateway, so that all traffic of a certain protocol (TCP, UDP, probably not ICMP heh) on a particular port or range of ports will be redirected to the internal IP address that the server is hosted upon.

In the documentation for the game, it should have a section on what ports need to be mapped. Otherwise, you can find that information about it online at the games website.
Quote:Original post by sordid
Most likely you'll need to set up a port mapping in your gateway, so that all traffic of a certain protocol (TCP, UDP, probably not ICMP heh) on a particular port or range of ports will be redirected to the internal IP address that the server is hosted upon.

In the documentation for the game, it should have a section on what ports need to be mapped. Otherwise, you can find that information about it online at the games website.



Sorry If I was unclear, when I said my game I meant the game I was trying to develop, therefore, no documentation that I dont already know(which is unfortunately, alot).
My Current Project Angels 22 (4E5)
I thought maybe you wanted to alter your gateway's NAT daemon itself to keep particular packets (possibly the ICMP Echo/Reply packets) unmolested.

At any rate.. what game are we talking about here? Most games usually have one udp port that needs to be remapped. DirectPlay-based games sometimes or usually require both -- I'm not entirely sure.. I code winsock only.
The game I am making is a 2D action game. I am writing it in C++ and using winsock for the networking with TCP as the protocol. I know that nothing is wrong with my client/server code, because it works in a LAN. My problem arose when I was trying to play the game with my friend. When I had him put in the IP address that the www.whatismyip.com site gave me, he still couldn't connect to the server I had created.
My Current Project Angels 22 (4E5)
Here's a few scenarios:

Your server computer is connected directly to the internet and is not firewalled: It should work.


Your server computer is connected to the internet and is firewalled: You will need to open the port your server runs off of.


Your server computer is connected to a gateway computer or a router which is connected to the internet: You will need to, on your gateway or router, map the port your server runs off of so that it redirects the traffic to the computer that you're running the server on. Either way, you'll have to consult the documentation/manual on your specific gateway/router on how to remap ports. You could tell us what it is, however.. somebody should be able to recap the process from memory.
The computer I was running the Server on is connected to a router wirelessly, and the firewall was turned off. So if I connect the server computer directly to the internet, bypassing the router, and then start my server, could my friend connect, even though he is behind a router?
My Current Project Angels 22 (4E5)
clients should be able to connect to the server even if the client is behind a firewall & gateway/router (provided that the client makes the first communication on any specific port to the server, and not vice versa)

however, if you absolutely cannot do a port mapping on the router, then you will have to connect the server computer directly to the internet.


This topic is closed to new replies.

Advertisement