简体   繁体   中英

How to use Dynamic IP to request connect with socket in c#?

public void connectContent()
{    
    client= new TcpClient();      
 
    var result=client.BeginConnect(IPAddress.Parse("192.168.1.4"), 12000,null,null);  
    var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
    
    if(success){
        Debug.Log("ok "+playerID);
    } else {
        Debug.Log("fail");
    }
}

I have a project developed for over 5 months, and it always can connect in the expected way.

But today it can't connect to server, after I changed the IP 192.168.1.3 to 192.168.1.4 , it works. I don't know why and how this happen. Static IP in code seems unsafe when some outside problem happen will influence the client IP.

How to use Dynamic IP rather than write the static IP in code?

Looking at your IP (192.168.xx.xx), you're working on a local.network.

You should use the device's host-name to connect to the device. If this is not possible: make the IP static by configuring the machine's IP or the DHCP entry .

Example with host name:

You can use the overload like this - make sure to substitute "player1" with the actual host name:

var result=client.BeginConnect("player1", 12000,null,null);

See: MSDN .


More context and explanation:

On a local.network, a device IP s might change over time, which is perfectly fine. It is not a problem or an error.

Devices have an IP, like "192.168.1.4", which might change. Devices will also have a host-name ; eg: "XperiaBox" or "RadioMediaStreamer".

The host-names on your.network can be found when you look onto the.network (eg; if you're on windows through windows explorer ->.network), or in your DHCP server (often your router).

If you use this host-name to connect to the device, the.network router (often acting as an DNS as well) will take care of resolving the IP for you. In that sense you easily avoid having to deal with the changing IPs.

So, Assuming you're on a local.network you will have a couple of options:

  • use the target machines host name and use this overload of BeginConnect . This will only work if the host name is known to the.network, or
  • make the "192.168.1.4" address static , so it never changes. You can configure this in the target machine's IP settings or in the DHCP server (based on host name or MAC address)

Other options on a local.network are possible, but I would avoid them because it's often an indication something is off.

If you're connecting to lets say high dynamic devices (like an app on mobile phones on you.network) - its better to configure the app on the phone to connect to the central system.


Using the host name will also work when you expand the usage to the public inte.net. In that case: it's easiest is to go with a static public IP and a DNS entry. It works the same, the DNS entry will act as your host-name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM