locked
Local IP address to global IP address RRS feed

  • Question

  • I am trying to make an online game server, the person who wrote this code for me is no longer available. He wrote this code so I could easily test my server on my local IP but didn't add anything to get my Global IP for when I start BETA testing.

    Here is the code I have at the moment and I need to change it so it will read my Global IP as well as my Local IP, any ideas would be a great help, I havent been using C# that long but I am learning quite fast xD

     

     

    System.Net.IPHostEntry LocalHE;

                LocalHE = System.Net.Dns.GetHostEntry(Dns.GetHostName());

                cmbListenIP.Items.Clear();

                foreach (IPAddress ipCurrent in LocalHE.AddressList)

                {

                    cmbListenIP.Items.Add(ipCurrent);

                    LocalServerIp = ipCurrent.ToString();

                }

                cmbListenIP.SelectedIndex = 0;

     

     

    Thanks, I really do appreciate any help with this.

    ps. This will eventually be going onto a VPS this is the main reason I need to find the Global IP address. I wouldn't mind even if I was using public static string = "IPADDRESS";    to get the IP address, just need a way to get that information.

    Friday, November 12, 2010 7:06 PM

Answers

  • As a followup because you said you already have your ports forwarded on your router:  You have your router set to forward port 123 to a local IP of 192.168.x.y.

     

    In that case, your server only needs to be listening on 192.168.x.y - because that is where your router is going to send the connection to.  Your server is not even aware (and does not need to be aware) that there is a global IP in play - only the client needs to know that.

    • Marked as answer by .Beatz Friday, November 12, 2010 9:31 PM
    Friday, November 12, 2010 8:08 PM

All replies

  • I don't know if enumerating the servers IP addresses will get the value inside global DNS. See here for other approaches

    http://stackoverflow.com/questions/3855450/external-dns-lookup-web-service-nslookup-dig

    Friday, November 12, 2010 7:28 PM
  • There are a couple different things that possibly come into play here.  First, is what is a "local" IP exactly?

    Is that the one corresponding to "localhost": 127.0.0.1

    Or, if you are behind a router which is doing NAT, then while the router has a "real-world" IP, while your machine has a "private IP" (sometimes 192.168.x.y or 10.x.y.z).

    If you are not worried about the NAT scenario, I would just call Dns.GetHostEntry("") (pass a BLANK string) - per the MSDN documentation at http://msdn.microsoft.com/en-us/library/ms143998.aspx:

    "When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host."

    That should give you enough information to populate what looks like a combo box for you.

     

    Separately, if you ARE worried about the NAT scenario - that is, your server has an IP address that is NOT visible to the outside world, then things get a bit more complicated.  In this scenario, it is the NAT/router that has a real IP (global ip?).  Your machine has no easy way to know what this is.

    A couple possibilities I see:

    - If this global IP has a hostname associated to it ("mygameserver.com"), you could perform a DNS lookup on that.

    - You could query some sort of service that is running outside the NAT for your IP.  The website http://www.whatismyip.org/ comes to mind, although I'm not sure who actually runs that or if you would want to rely on it being there.  You could potentially put up your own service outside the NAT that effectively does the same thing.

    - Let the user manually enter it?  Either live in the app or in some sort of configuration file.

     

    Hardcoding it seems like a bad solution as IP's can change over time, and it would then require a recompilation to change.

    • Marked as answer by .Beatz Friday, November 12, 2010 7:50 PM
    • Unmarked as answer by .Beatz Friday, November 12, 2010 8:01 PM
    Friday, November 12, 2010 7:38 PM
  • Yeah by Local IP I mean 192.168.x.y and Global I mean Routers IP address 82.31.ccc.dd

     

    The option for the user selecting the IP is totally out of the question as I need to for the server to be able to run through the correct IP.

    The IP that I need to use is my Routers "real world IP" thus being easier to hardcode it into the source as I already know what the IP is. Also if this does change as it is hardcoded in I can simply change the string? that holds the IP address?

     

    The end user has a game client that connects straight to the IP that is hardcoded into their client which has to be my Routers IP (Global IP) else they cant connect to the Game Server. I have ports forwarded so they can access my PC through the router.

     

    I don't really need the combo box there it was just so I could easily select the "local" IP that I wanted to use.

     

    Sorry if I am quite vague I am no good at explaining things.

    Friday, November 12, 2010 8:01 PM
  • Let me ask a different question:  What are you actually going to do with this IP?


    If you are trying to tell your socket what port to listen on, could you just use IPAddress.Any?  Do you really CARE to limit it to a particular interface?

    Even if you had the global IP, you would not be able to bind your Listen to that IP as your server doesn't actually have that IP.

    Friday, November 12, 2010 8:06 PM
  • As a followup because you said you already have your ports forwarded on your router:  You have your router set to forward port 123 to a local IP of 192.168.x.y.

     

    In that case, your server only needs to be listening on 192.168.x.y - because that is where your router is going to send the connection to.  Your server is not even aware (and does not need to be aware) that there is a global IP in play - only the client needs to know that.

    • Marked as answer by .Beatz Friday, November 12, 2010 9:31 PM
    Friday, November 12, 2010 8:08 PM
  • As a followup because you said you already have your ports forwarded on your router:  You have your router set to forward port 123 to a local IP of 192.168.x.y.

     

    In that case, your server only needs to be listening on 192.168.x.y - because that is where your router is going to send the connection to.  Your server is not even aware (and does not need to be aware) that there is a global IP in play - only the client needs to know that.

    I think that is where I have been going wrong then, I will try this out and reply when I have tested it out.

     

    Thanks for the help 

    Friday, November 12, 2010 9:15 PM
  • Thanks for the help, I have got it to work now.

     

    Cabadam Thanks you so much :)

    Friday, November 12, 2010 9:31 PM