gpt4 book ai didi

c# - 如何修复 "a connection attempt failed because the connected party did not properly respond after a period of time ..."错误?

转载 作者:可可西里 更新时间:2023-11-01 02:31:39 26 4
gpt4 key购买 nike

我正在用 C# 制作游戏,我想显示对手的进度( Action 等)。所以我通过 TCP 协议(protocol)将游戏中的事件发送给对手。

我已经在本地主机上尝试了我的应用程序并且它可以工作但是当我尝试使用我的外部地址以便通过 Internet 进行通信时我在类 TcpInformer.Connect() 中收到以下错误:

a connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond (my external IP address):(port)

我认为问题出在我落后于 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(我的程序的第二个实例),这在一台只有一个互联网连接的计算机上是不可能的。

关于c# - 如何修复 "a connection attempt failed because the connected party did not properly respond after a period of time ..."错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1187783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com