目录
前言
我从 MSDN 的类库示例中获取这些代码。所以它必须正常工作。是的,我是部分正确的。在我的电脑上运行时它成功了。但是通过互联网,它失败了。我正在接受 ConnectionRefused SocketErrorCode 每一次。
下面我会给出源代码,最初,你必须知道我修改过的测试。
For Server Code:
But it failed
服务器端代码
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();
}
}
}
本地计算机示例(成功)
我只在我的电脑上试过。首先我打开 ServerTest 程序然后我运行 ClientText 程序。
客户端输出
Started...
İP Address:Sent hello
Received HELLO
Hit enter to continue
服务器端输出
Waiting for a connection...
Connected: 192.168.1.3:13000
Received: hello
Sent: HELLO
Waiting for a connection
互联网网络示例(未成功)
我和我的 friend 一起试过,他的 IP 地址是“88.226.0.218”。他从whatismyipaddress.com得知他的IP地址我运行 ClientTest 程序,他运行 ServerTest 程序。输出如下:
客户端输出
Started...
İP Address:
88.226.0.218
SocketErrorCode: ConnectionRefused
Hit enter to continue
服务器端输出
Waiting for a connection...
没事了。 ServerTest 程序只等待。
我的可能性考虑
任何帮助,将不胜感激。这对我来说非常重要。由于这次失败没有成功,我已经将我的项目推迟了将近一个月。
最佳答案
要尝试的一件事是在同一网络上的两台计算机之间进行测试。如果可行,您可以将注意力从 上移开。您的 电脑是问题。
无论如何,罪魁祸首很可能是防火墙,正如克里斯托弗所说。实际上,我会先检查您 friend 的防火墙,因为有些(大多数?)防火墙不会阻碍传出连接。
此外,如果您的 friend 在路由器后面,您需要让他将端口 13000 转发到他计算机的本地地址(很可能是 192.168.1.x)上的端口 13000。
可以在此处找到有关防火墙和端口转发的更多信息(和指南!):http://portforward.com/help/start_here.htm
关于.net - 通过互联网连接套接字失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8195421/