C# 网络 : Socket won't respond to external ip

标签 c# sockets networking console-application

我正在做一些 socket 测试。我正在尝试将客户端连接到服务器的外部/公共(public) IP 地址。不幸的是,虽然我找到了一个开放的端口,但套接字并没有响应它的外部/公共(public) ip。这是服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace PortScannerServer
{
   class Program
   {
        static void Main(string[] args)
        {
            Console.Title = "Server";
            while (true)
            {
                try
                {
                    _server.Bind(new IPEndPoint(IPAddress.Any, 0));
                    _server.Listen(10);
                    Console.WriteLine(_server.LocalEndPoint.ToString());
                    Console.WriteLine(GetExternalAddress().ToString());
                    _server.Accept();
                    Console.WriteLine("Connected");
                    break;
                }

                catch (Exception ex)
                {
                     Console.WriteLine(ex.Source + ":" + ex.Message + ":" + ex.InnerException);
                }
            }
        }

        static IPAddress GetExternalAddress()
        {
            var html = new WebClient().DownloadString("http://checkip.dyndns.com/");
            var ipStart = html.IndexOf(": ", StringComparison.OrdinalIgnoreCase) + 2;
            return IPAddress.Parse(html.Substring(ipStart, html.IndexOf("</", ipStart, StringComparison.OrdinalIgnoreCase) - ipStart));
        }

        static Socket _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    }
}

这是客户端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace PortScanner
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Client";
            Console.WriteLine("Enter host ip");
            _ip = IPAddress.Parse(Console.ReadLine());
            Console.WriteLine("Enter host port");
            _port = Convert.ToInt32(Console.ReadLine());

            while (true)
            {
                try
                {
                    _client.Connect(new IPEndPoint(_ip, _port));
                    Console.WriteLine("Connected!");
                    Console.ReadLine();
                    break;
                }

                catch
                {
                    Console.WriteLine("Could not connect to client");
                }
            }
        }

        static IPAddress _ip;

        static int _port;

        static Socket _client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    }
}

如果我将 ip 地址设置为本地 ip(127.0.0.1 或 192.168.1.1),我可以让客户端连接,但是,当我将 ip 地址设置为外部/公共(public) ip 时,它不会连接。有谁知道如何解决这一问题?

最佳答案

根据您的描述,您尝试连接的服务器似乎与客户端位于同一内部网络上(您所说的 127.0.0.1 工作的事实让我认为它甚至可能是同一台机器)。

要支持导航到公共(public)地址然后转发到始发网络中的内部地址,您的 NAT 路由器需要支持称为“Hairpinning”或“NAT Loopback”的功能。许多消费者 SOHO 路由器不支持此功能,并且那些不经常在文档中标注是否支持该功能的路由器。

您需要将路由器上的固件升级/更换为支持发夹的固件或完全更换路由器 with one that does能够将您的公共(public) IP 重定向到路由器后面的内部网络上的计算机。

除了不使用公共(public) IP 而是使用内部 IP 之外,您无法在代码中解决此问题。

关于C# 网络 : Socket won't respond to external ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42422609/

相关文章:

c# - 将数据保存在 Form2 中,帐户 ID 位于 1 列中,以查看谁拥有该数据

c# - 使用 Linq to XML (C#) 如何查找属性值?

python - Python中的socket.SOL_SOCKET和socket.SO_REUSEADDR有什么作用

linux - 如何让 ng 服务在启动后放弃 root 权限?

Azure Vnet 专用 IP 范围

c# - WPF 中的命令绑定(bind)无法读取属性值

c# - 将控件添加到上下文菜单

mysql - 使用 sourcemod 时无法通过套接字 '/tmp/mysql.sock' 连接到本地 MySQL 服务器

ruby-on-rails - Rails S挂起,端口未断开连接?

macos - 如何在ASyncSocket中检测远程断开连接