Click here to Skip to main content
14,734,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a project which a server will communicate with multiclients. The program is working fine when sever and clients connect in same network (same ip address and same port). However, I need to change my server ip address every single time I move to different place. Is there any possible way to let the server and client always connected even they are in different network? Should I change the server ip address to static and how to do it programmatically? Any example and guide could be appreciated.


What I have tried:

I have tried this code to change my sever ip address static but I still have to change my code if I move to other place.

private void setIP(string ip_address)
  ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
   ManagementObjectCollection objMOC = objMC.GetInstances();

   foreach (ManagementObject objMO in objMOC)
         ManagementBaseObject setIP;
         ManagementBaseObject newIP = objMO.GetMethodParameters("EnableStatic");
         newIP["IPAddress"] = new IPAddress[] { IPAddress.Parse(ip_address) };
        setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
      catch (Exception ex)
Updated 10-Sep-20 0:21am
Richard MacCutchan 10-Sep-20 5:17am
IP addresses are controlled by the network provider. Talk to them for the allocation of a static address.

1 solution

If you're switching between networks then you can absolutely expect your IP address to be continually changing. Traditionally a server wouldn't be moving around, it would sit on a network somewhere statically and the network provider can provide a static IP address.

One way around this would be to use a "dynamic DNS" service, which is basically a DNS host name provider which requires a client installing on the "server" machine. Then when the IP address is changed, it issues an update to the service to provide the new IP address. The clients can connect to the host name issued by the provider.

In reality, if this server is in development at the moment and will eventually be ready for production use you will probably be deploying it onto a service provider (AWS, Azure etc.) which do provide static access points.
Member 14934537 11-Sep-20 0:32am
Thanks for the solution. Client and server can connect in different network only if there are a new DNS provided by service provider?
Chris Copeland 11-Sep-20 4:33am
That would be an optimal solution if your server is going to be moving around and switching networks. If you Google "dynamic dns" you'll find some recommendations. I saw another solution involving setting up a VPN on your router, but if you're switching networks then that probably defeats the purpose.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900