如何解决“连接尝试失败,因为连接方在一段时间后没有正确响应......”错误?
Posted
技术标签:
【中文标题】如何解决“连接尝试失败,因为连接方在一段时间后没有正确响应......”错误?【英文标题】:How to fix "a connection attempt failed because the connected party did not properly respond after a period of time ..." error? 【发布时间】:2010-11-14 08:08:21 【问题描述】:我正在用 C# 制作游戏,我想显示对手的进度(动作等)。所以我通过TCP协议将游戏中的事件发送给对手。
我已经在 localhost 上尝试过我的应用程序并且它可以工作,但是当我尝试使用我的外部地址通过 Internet 进行通信时,我在类 TcpInformer.Connect() 中收到以下错误:
连接尝试失败,因为连接方在 一段时间,或建立连接失败,因为连接的主机未能 响应(我的外部 IP 地址):(端口)
我认为问题在于我落后于 NAT。但我已经为 IP 10.0.0.1 上的端口 49731 设置了端口转发,没有任何改变。
我的第二个猜测是 Windows 防火墙,但即使我停止了防火墙,我的应用程序也没有开始工作。
我的两台电脑连接代码是:
TcpInformer peer;
TcpHost server;
public void PrepareConnection() // for server (host)
playerType = PlayerType.One;
server = new TcpHost(form, this);
server.Start("10.0.0.1", 49731);
public void PrepareConnection2() // for client
playerType = PlayerType.Two;
peer = new TcpInformer(form, this);
peer.Connect("MY EXTERNAL IP", 49731);
// classes TcpHost and TcpInformer
public interface ITcpCommunication
#region Operations (3)
void ReadData();
void SendData(byte[] message);
void SendData(byte[] message, int size);
#endregion Operations
public class TcpInformer : ITcpCommunication
#region Fields (9)
private NetworkStream con_ns;
private TcpClient con_server;
private bool connected;
private Fmain form;
private SecondPlayer player;
private int port;
private string server;
private string stringData;
#endregion Fields
#region Delegates and Events (1)
// Events (1)
public event SimulationEventHandler ReadEvent;
#endregion Delegates and Events
#region Constructors (1)
public TcpInformer(Fmain form, SecondPlayer player)
this.form = form;
connected = false;
this.player = player;
#endregion Constructors
#region Methods (6)
// Public Methods (5)
///
///
///
/// e.g., server = "127.0.0.1"
/// e.g., port = 9050
public void Connect(string server, int port)
this.port = port;
this.server = server;
connected = true;
try
con_server = new TcpClient(this.server, this.port);
catch (SocketException ex)
connected = false;
MessageBox.Show("Unable to connect to server" + ex.Message);
return;
con_ns = con_server.GetStream();
public void Disconnect()
form.Debug("Disconnecting from server...", "Player2Net");
con_ns.Close();
con_server.Close();
public void ReadData()
if (con_ns != null)
if (con_ns.DataAvailable)
byte[] data = new byte[1200];
int received = con_ns.Read(data, 0, data.Length);
player.ProcessReceivedData(data, received);
else
form.Debug("Warning: con_ns is not inicialized.","player2");
public void SendData(byte[] message)
con_ns.Write(message, 0, message.Length);
con_ns.Flush();
public void SendData(byte[] message, int size)
if (con_ns != null)
con_ns.Write(message, 0, size);
// Private Methods (1)
private void Debug(string message)
form.Debug("Connected to: " + server + "port: " + port.ToString() + ": " + message, "Player2Net");
#endregion Methods
public class TcpHost : ITcpCommunication
#region Fields (9)
private ASCIIEncoding enc;
private Fmain form;
private TcpListener listener;
private SecondPlayer player;
private int port;
private Socket s;
private string server;
private bool state;
#endregion Fields
#region Delegates and Events (1)
// Events (1)
public event SimulationEventHandler ReadEvent;
#endregion Delegates and Events
#region Constructors (1)
public TcpHost(Fmain form, SecondPlayer player)
this.player = player;
this.form = form;
state = false;
enc = new ASCIIEncoding();
#endregion Constructors
#region Methods (5)
// Public Methods (5)
public void Close()
state = false;
s.Close();
listener.Stop();
public void ReadData()
if (state == true)
if (s.Available > 0) // if there's any data
byte[] data = new byte[1200];
int received = s.Receive(data);
player.ProcessReceivedData(data, received);
public void SendData(byte[] message)
if (state == true)
s.Send(message);
public void SendData(byte[] message, int size)
if (state == true)
s.Send(message, size, SocketFlags.None);
public void Start(string p_ipAddress, int listenPort)
//IPAddress ipAddress = IPAddress.Loopback
IPAddress ipAddress = IPAddress.Parse(p_ipAddress);
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, listenPort);
//listener = new TcpListener(ipAddress, listenPort);
listener = new TcpListener(ipLocalEndPoint);
server = "[provider]";
port = listenPort;
listener.Start();
form.Debug("Server is running", "Player1Net");
form.Debug("Listening on port " + listenPort, "Player1Net");
form.Debug("Waiting for connections...", "Player1Net");
s = listener.AcceptSocket();
form.Debug("Connection accepted from " + s.RemoteEndPoint, "Player1Net");
state = true;
#endregion Methods
有没有办法检查哪里出了问题? 非常感谢您的帮助!
【问题讨论】:
我发现了问题所在。我在 10.0.0.1 上收听并试图访问我的外部 IP(我的程序的第二个实例),这在只有一个互联网连接的计算机上是不可能的。我应该删除这篇文章吗? 添加您自己的答案来解释解决方案,并将其标记为已接受。 离开这篇文章。这对我很有帮助。 【参考方案1】:我发现了问题所在。我在 10.0.0.1 上收听并试图访问我的外部 IP(我的程序的第二个实例),这在只有一个互联网连接的计算机上是不可能的。
【讨论】:
【参考方案2】:我在 AWS *** 中也遇到了同样的问题。
我将 proxy.company.com(外部 ip)更改为 10.0.0.5。
现在可以使用了。
【讨论】:
以上是关于如何解决“连接尝试失败,因为连接方在一段时间后没有正确响应......”错误?的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE[HY000] [2002] 连接尝试失败,因为连接方在一段时间后没有正确响应,
System.Net.Sockets.SocketException (10060):连接尝试失败,因为连接方在一段时间后没有正确响应