通过Internet网络连接套接字失败

Mes*_*sut 5 .net sockets

目录

  • 前言
  • Serverside代码
  • 客户代码
  • 本地计算机示例(成功)
  • İnternet网络示例(Unsuccessfull)
  • 我的可能性考虑因素

前言

我从MSDN的类库示例中获取这些代码.所以它必须正常工作.是的,我部分正确.在我的计算机上运行时它会成功.但是在互联网网络上,它很脆弱.我每次都在使用ConnectionRefused SocketErrorCode.
我将在下面给出源代码,最初,你必须知道我修改过的测试.

对于服务器代码:

  • 我使用IPAddress.IPv6Any而不是IPAddress.Any.
  • 我也试过没有server.AllowNatTraversal(true)

但它失败了

Serverside代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ServerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = null;
            try
            {
                int port = 13000;
                server = new TcpListener(IPAddress.Any, port);
                server.AllowNatTraversal(true);
                server.Start();
                byte[] bytes = new byte[256];
                string data = null;
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected: {0}", client.Client.LocalEndPoint.ToString());
                    data = null;
                    NetworkStream stream = client.GetStream();
                    int i;
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);
                        data = data.ToUpper();
                        byte[] msg = Encoding.ASCII.GetBytes(data);
                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", data);
                    }
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Socket exception message: {0}", e.Message);
                Console.WriteLine("Socket Error Code: {0}", e.SocketErrorCode.ToString());
            }
            finally
            {
                server.Stop();
            }
            Console.WriteLine("\nHit enter to continue...");
            Console.ReadKey();            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

客户代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Started...");
            Console.WriteLine("?P Address:");
            string hostName = Console.ReadLine();
            if(hostName.Length == 0)
            {
                hostName = Dns.GetHostName();
            }
            Connect(hostName, "hello");
        }

        static void Connect(string server, string message)
        {
            try
            {
                int port = 13000;
                TcpClient client = new TcpClient(server, port);
                byte[] data = Encoding.ASCII.GetBytes(message);
                NetworkStream stream = client.GetStream();
                stream.Write(data, 0, data.Length);
                Console.WriteLine("Sent: {0}", message);
                data = new byte[256];
                string responseData = string.Empty;
                int bytes = stream.Read(data, 0, data.Length);
                responseData = Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e.Message);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Socket Error Code: {0}", e.SocketErrorCode.ToString());
            }
            Console.WriteLine("Hit enter to continue...");
            Console.ReadKey();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

本地计算机示例(成功)

我只在我的电脑上试过.首先,我打开了ServerTest程序,然后运行了ClientText程序.

客户输出

开始...
İP地址:

发送你好
收到HELLO命令
进入继续

Serverside输出

正在等待连接...
已连接:192.168.1.3:13000
收到:你好
发送:HELLO
等待连接

İnternet网络示例(Unsuccessfull)

我和我的朋友试了一下,他的IP地址是"88.226.0.218".他从whatismyipaddress.com学到了他的ip地址我运行了ClientTest程序并运行了ServerTest程序.输出如下:

客户输出

开始...
İP地址:
88.226.0.218
SocketErrorCode:ConnectionRefused
点击进入继续

Serverside输出

等待连接......

没别的了.ServerTest程序只等待.

我的可能性考虑因素

  • 当我使用İPAddress.İPv6Any时,它只能使用İPv6
  • 我的调制解调器防火墙可以阻止NatTraversal.如果是这样,我如何以编程方式启用调制解调器的NatTraversal?
  • AllowNatTraversal的方法的TcpListener类,都配备了.NET4,不能正常工作.(我不相信,但为了可能性)

任何帮助,将不胜感激.这对我来说非常重要.由于没有成功,我几乎将我的项目推迟了一个月.

Xce*_*led 0

要尝试的一件事是在同一网络上的两台计算机之间进行测试。如果有效,您可以将注意力从出现问题的计算机上移开。

无论如何,正如克里斯托弗所说,罪魁祸首很可能是防火墙。实际上,我会先检查您朋友的防火墙,然后再检查您自己的防火墙,因为某些(大多数?)防火墙不会阻碍传出连接。

此外,如果您的朋友位于路由器后面,您需要让他将端口 13000 转发到其计算机的本地地址(很可能是 192.168.1.x)的端口 13000。

有关防火墙和端口转发的更多信息(和指南!)可以在这里找到: http: //portforward.com/help/start_here.htm