C#联网:Socket赢了';不响应外部ip

C#联网:Socket赢了';不响应外部ip,c#,sockets,networking,console-application,C#,Sockets,Networking,Console Application,我正在做一些插座测试。我正在尝试将客户端连接到服务器的外部/公共ip地址。不幸的是,虽然我发现了一个开放端口,但套接字没有响应其外部/公共ip。以下是服务器代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; namespa

我正在做一些插座测试。我正在尝试将客户端连接到服务器的外部/公共ip地址。不幸的是,虽然我发现了一个开放端口,但套接字没有响应其外部/公共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);
    }
}

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

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

要支持导航到公共地址,然后转发到原始网络中的内部地址,NAT路由器需要支持名为“”或“”的功能。许多消费者SOHO路由器不支持此功能,而那些不支持此功能的路由器也不经常在其文档中添加标签

您需要将路由器上的固件升级/更换为支持发夹的固件,或者完全更换路由器,以便将公用IP重定向到路由器后面的内部网络上的计算机


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

您是否已将端口转发到您的内部IP地址?是的。我获取了服务器控制台应用程序显示的可用端口和外部ip,并使用该端口尝试连接到服务器。您是否尝试使用Telnet与外部ip和端口测试连接?否,我没有。服务器和客户端是否都位于NAT后面的同一内部LAN上?好的,谢谢!如果我想创建一个应用程序,其中一个用户使用他们的外部ip连接到另一个用户,那么两个用户是否仍然需要他们的路由器来支持发夹,即使他们在不同的网络上?不,如果他们在不同的网络上,您将不需要发夹支持。只有当两个用户都在同一个NAT后面连接到该NAT的公共IP时,您才需要发夹支持。因此,如果他们在不同的网络上,他们可以连接?只要充当“服务器”的人的端口转发正确,就可以。
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);
    }
}