目录
- Preface
- 服务器端代码
- 客户端代码
- 本地计算机示例(成功的)
- 互联网网络示例(不成功)
- 我的可能性考虑
Preface
我从 MSDN 的类库示例中获取这些代码。所以它必须正确工作。是的,我说对了一部分。在我的电脑上运行就成功了。但通过互联网,它失败了。我在拿连接被拒绝 套接字错误代码每一次。
下面我将给出源代码,首先,你必须知道我修改的测试。
对于服务器代码:
- I used IP 地址.IPv6Any代替IP地址.任意.
- 我也尝试过没有server.AllowNatTraversal(true)
但它失败了
服务器端代码
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();
}
}
}
客户端代码
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();
}
}
}
本地计算机示例(成功的)
我只在我的电脑上尝试过。首先我打开服务器测试然后我跑了程序客户文本程序。
客户端输出
开始...
IP地址:
发送了你好
收到你好
按 Enter 键继续
服务器端输出
正在等待连接...
连接:192.168.1.3:13000
收到: 你好
发送: 你好
等待连接
互联网网络示例(不成功)
我和我的朋友一起尝试过,他的IP地址是“88.226.0.218”。他从以下位置得知了自己的 IP 地址Whatismyipaddress.com http://whatismyipaddress.com/ I ran 客户端测试程序然后他跑了服务器测试程序。输出如下:
客户端输出
开始...
IP地址:
88.226.0.218
套接字错误代码:连接被拒绝
按 Enter 键继续
服务器端输出
正在等待连接...
没事了。服务器测试程序只会等待。
我的可能性考虑
- 我使用时只能使用 ipv6?PAddress.?Pv6Any
- 我的调制解调器防火墙可能阻止 NatTraversal。如果是这样,我如何以编程方式启用调制解调器的 NatTraversal?
-
允许Nat遍历的方法TCP监听器类,附带.Net4,无法正常工作。 (我不相信,但为了可能性)
任何帮助,将不胜感激。这对我来说非常重要。由于这次失败,我的项目已经推迟了将近一个月。