如何使用动态外部IP地址在Internet上建立客户端和服务器之间的连接? C# [英] How to establish a connection between a client and server on the internet with dynamic external ip-address ? c#

查看:145
本文介绍了如何使用动态外部IP地址在Internet上建立客户端和服务器之间的连接? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台带有动态外部IP地址的服务器。如何在互联网上创建连接?之后,客户端知道外部IP地址,必须连接。



我尝试过:



I have a server with dynamic external IP-address. How to create a connection on the internet ? After that, the client, knowing external IP-address, has to connect.

What I have tried:

UPnPNAT upnpnat = new UPnPNAT();
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
            IPAddress ipAddress = null; 
            IPEndPoint Addr = null; 
            NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
            mappings.Add(20131, "TCP", 20131, "46.175.163.14", true, "Test Open Port");
            Addr = new IPEndPoint(IPAddress.Parse("192.168.0.111"), 20131); //"localhost" = 127.0.0.1

            s.Bind(Addr);

            s.Listen(10);

推荐答案

客户端必须知道地址。我建议你改变一个静态的动态。否则,将ip地址放在配置文件中,然后在每次更改时更新,这样您就不必每次都重新编译代码。
The client will have to know the address. I suggest you change your dynamic one for a static one. Otherwise, put the ip address in a config file and then update that each time it changes so you don't have to recompile the code every time.


除了Ryan发布的解决方案之外,您还可以使用动态DNS - 维基百科,免费的百科全书 [ ^ ]服务。大多数互联网路由器和调制解调器提供了一个选项,可以在它发生变化时将动态IP地址传递给这样的服务。
Besides the solution posted by Ryan you can use a Dynamic DNS - Wikipedia, the free encyclopedia[^] service. Most internet routers and modems provide an option to pass their dynamic IP address to such a service when it has changed.


这篇关于如何使用动态外部IP地址在Internet上建立客户端和服务器之间的连接? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆