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

时间:2011-11-19 16:26:41

标签: .net sockets

目录

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

前言

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

  

对于服务器代码:

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

但它失败了

Serverside Code

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();            
        }
    }
}

Clientside Code

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();
        }
    }
}

本地计算机示例( Successfull

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

客户输出

  

...发起者
  İP地址:

     

发送你好了   收到你好   按Enter键继续

Serverside输出

  

等待连接...
  已连接:192.168.1.3:13000
  收到了:你好   发送:你好   等待连接

İnternet网络示例( Unsuccessfull

我和我的朋友试了一下,他的IP地址是“88.226.0.218”。他从whatismyipaddress.com了解了他的IP地址,我运行了 ClientTest 程序,并运行了 ServerTest 程序。输出如下:

客户输出

  

...发起者
  İP地址:
  88.226.0.218
  SocketErrorCode:ConnectionRefused
  按Enter键继续

Serverside输出

  

等待连接......

没什么。 ServerTest 程序仅等待。

我的可能性考虑因素

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

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

1 个答案:

答案 0 :(得分:0)

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

无论如何,罪魁祸首很可能是防火墙,克里斯托弗说。我会在你自己之前检查你朋友的防火墙,因为一些(大多数?)防火墙不会阻碍传出连接。

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

有关防火墙和端口转发的更多信息(和指南!),请访问:http://portforward.com/help/start_here.htm