首页 > 解决方案 > 无法通过 Internet 创建 TCP/IP 连接

问题描述

TCP/IP 和 PORT 转发的问题

  1. 我禁用了防火墙
  2. 将DMZ设置为我的本地 IP
  3. 运行了一个 C# 控制台应用程序,它侦听来自端口 8659(服务器)的传入连接:
static void Main(string[] args)
{
    // port to listen to
    int connPort = 8659;
    // the  local endPoint
    var localEP = new IPEndPoint(IPAddress.Any, connPort);
    // creating the socket
    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
    {
        // binding the socket to the local endPoint
        socket.Bind(localEP);
        // listening for incoming connections
        socket.Listen(10);
        // logging
        Debug.WriteLine("waiting for client ...");
        // grab the first client
        var client = socket.Accept();
        // logging
        Debug.WriteLine("client connected !");
        var jsonContent = "this is json test";
        // sending the json content as a json response
        client.Send(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\n" +
            "Content-Type: text/plain\n" +
            $"Content-Length: {jsonContent.Length}\n" +
            "\n" + jsonContent));
    } // end of using
    Console.ReadLine();
} // end of main
  1. 尝试使用公共 IP 与另一个应用程序(客户端)连接到服务器:
static void Main(string[] args)
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    // the ip address of the interface connected to internet
    var ip = host.AddressList[1];
    // my public IP ( dynamic ) not yet static, just a test
    var ServerIP = IPAddress.Parse("197.119.200.25");
    // the server endpoint
    var serverEP = new IPEndPoint(ServerIP, 8659);
    // the endpoint to use to connect to the server
    var localEP = new IPEndPoint(ip, 8658);
    // creating the socket
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    // bind the socket to the endpoint
    socket.Bind(localEP);
    // connect to the server endpoint
    socket.Connect(serverEP);
} // end of mainc

客户抛出:

System.Net.Sockets.SocketException: '连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接主机没有响应 197.119.200.25:8659'

所以我的问题是为什么机器的公共 IP 无法访问我的服务器应用程序?

我不知道是我的 ISP 阻塞了端口,还是我有另一个我不知道的问题!

标签: c#socketstcpportforwardingdmz

解决方案


推荐阅读