连接 2 台不同子网的计算机

发布于 2024-08-14 02:02:18 字数 312 浏览 11 评论 0 原文

如何连接两台不同子网的计算机?例如,假设如下:

192.168.1.92 连接到外部可见的 222.251.155.20。 192.168.1.102 连接到外部可见的 223.251.156.23。

现在两台机器都连接到中间人服务器,这样它们就可以协商彼此的内部和外部 IP,并让其中一个打开侦听端口。我目前知道如何做到这一点的唯一方法是端口转发。

我相当精通 C# 套接字,只是不知道如何连接位于两个不同子网上的两台计算机。

我有一个客户端连接到的服务器,该服务器有一个域名,通常客户端将位于家庭路由器后面,我希望它们能够直接相互共享信息。

How do I connect 2 different subnet’ed computers? For example sake let’s say the following:

192.168.1.92 connected to externally visible 222.251.155.20.
192.168.1.102 connected to externally visible 223.251.156.23.

Now there is middle man server that both machines are connected to so they can negotiate each other’s internal and external IP’s and have one open up a listening port. The only way I currently know how to do this is port forwarding.

I’m fairly proficient with C# sockets, just don’t how to connect two computers that are on two different subnets.

I have a server that the clients connect to that has a domain name, usally the clients will be behind home routers and I want them to be able to share information directly with each other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-08-21 02:02:18

您要寻找的是NAT 穿越
没有中继服务器和端口转发的解决方案通常使用某种形式的 UDP 打洞
标准化机制是 STUN (即 STUN) org/wiki/Interactive_Connectivity_Establishment" rel="nofollow noreferrer">交互式连接建立)。

注意:通过 UDP 实现 UDP 打洞和可靠的文件传输并非易事。最好的选择可能是使用 UPnP 或 NAT-PMP 自动设置端口转发。两者都有库,例如 Mono.Nat (来源):

class NatTest
{
    public Start ()
    {
        // Hook into the events so you know when a router
        // has been detected or has gone offline
        NatUtility.DeviceFound += DeviceFound;
        NatUtility.DeviceLost += DeviceLost;

        // Start searching for upnp enabled routers
        NatUtility.StartDiscovery ();
    }

    void DeviceFound(object sender, DeviceEventArgs args)
    {
        // This is the upnp enabled router
        INatDevice device = args.Device;

        // Create a mapping to forward external port 3000 to local port 1500
        device.CreatePortMap(new Mapping(Protocol.Tcp, 1500, 3000));

        // Retrieve the details for the port map for external port 3000
        Mapping m = device.GetSpecificMapping(Protocol.Tcp, 3000);

        // Get all the port mappings on the device and delete them
        foreach (Mapping mp in device.GetAllMappings())
            device.DeletePortMap(mp);

        // Get the external IP address
        IPAddress externalIP = device.GetExternalIP();
    }

    private void DeviceLost (object sender, DeviceEventArgs args)
    {
        INatDevice device = args.Device;

        Console.WriteLine ("Device Lost");
        Console.WriteLine ("Type: {0}", device.GetType().Name);
    }
}

What you're looking for is NAT traversal.
Solutions without a relaying server and without port forwarding usually use some form of UDP hole punching.
A standardized mechanism is STUN (i.e. Interactive Connectivity Establishment).

Note: implementing UDP hole punching and reliable file transfer over UDP is not trivial. The best option is probably to automatically set up port forwarding with UPnP or NAT-PMP. There are libraries for both, e.g., Mono.Nat (sources):

class NatTest
{
    public Start ()
    {
        // Hook into the events so you know when a router
        // has been detected or has gone offline
        NatUtility.DeviceFound += DeviceFound;
        NatUtility.DeviceLost += DeviceLost;

        // Start searching for upnp enabled routers
        NatUtility.StartDiscovery ();
    }

    void DeviceFound(object sender, DeviceEventArgs args)
    {
        // This is the upnp enabled router
        INatDevice device = args.Device;

        // Create a mapping to forward external port 3000 to local port 1500
        device.CreatePortMap(new Mapping(Protocol.Tcp, 1500, 3000));

        // Retrieve the details for the port map for external port 3000
        Mapping m = device.GetSpecificMapping(Protocol.Tcp, 3000);

        // Get all the port mappings on the device and delete them
        foreach (Mapping mp in device.GetAllMappings())
            device.DeletePortMap(mp);

        // Get the external IP address
        IPAddress externalIP = device.GetExternalIP();
    }

    private void DeviceLost (object sender, DeviceEventArgs args)
    {
        INatDevice device = args.Device;

        Console.WriteLine ("Device Lost");
        Console.WriteLine ("Type: {0}", device.GetType().Name);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文