gpt4 book ai didi

c# - TcpClient - 现有连接被远程主机强行关闭

转载 作者:太空狗 更新时间:2023-10-30 00:31:35 25 4
gpt4 key购买 nike

信息

我一直在用 C# 开发 Web http 服务器,并决定添加远程控制台功能。可以从任何位置使用控制台,并使用 TcpListener(Web 服务器)和 TcpClient(远程控制台)发送命令和函数。

代码

这是我的服务器的样子:

TcpListener consoleListener = new TcpListener(consolePort);
consoleListener.Start();
byte[] bytes = new Byte[256];
string data = null;
while (true)
{
TcpClient client = consoleListener.AcceptTcpClient();
data = null;
byte[] msg = { 0 };
int i;
while ((i = client.GetStream().Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
if (data == "shutdown")
{
//Server shutdown logic.
}
//Other commands here...
else
{
msg = Encoding.ASCII.GetBytes("Invalid command. Type 'help' or '?' to get a list of commands.");
}
client.GetStream().Write(msg, 0, msg.Length); //sends return message to console
}
client.Close(); //closes connection between client and server after EVERY command. Connection is reopened when a new command is sent.
}

注意 - 服务器在网络服务器和主控制台应用程序线程的单独线程上运行。

这是我的客户:

public static string Communicate(string text)
{
try
{
TcpClient client = new TcpClient(ip, port); //initializes tcpclient (ip and port are correct)

byte[] data = System.Text.Encoding.ASCII.GetBytes(text); //converts text to bytes for stream writing

NetworkStream stream = client.GetStream();

stream.Write(data, 0, data.Length);

Console.WriteLine("Sent data: " + text);

data = new Byte[256];

string responseData = String.Empty; //initializes responsData string

Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
client.Close();
return responseData; //returns what server writes
}
catch (Exception ex)
{
return "An error occured\n" + ex.ToString();
}
}

问题

我可以向服务器发送一条命令并成功返回。但是,当我尝试发送另一个命令时,服务器抛出以下错误:

System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at ---.Server.ConsoleListener() in X:\Users\---\Documents\Visual Studio 2013\Projects\---\---\Program.cs:line x

我知道这不是防火墙或管理员提升问题,因为我可以成功发送一个命令。只有在发送第二个命令时才会抛出此错误。

这是描述问题的屏幕截图: The remote console and server communication and error reporting.

编辑:通过做一些研究,我发现问题很可能是我的 for 循环中的一个小错误造成的。但是,我不知道解决这个问题的任何方法,因为我不知道确切的问题:)。请帮助我识别它,以便我修复它。

再次感谢

最佳答案

您的客户端似乎在一条消息后关闭了连接。

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
client.Close();
return responseData; //returns what server writes

如果你想保持连接,你应该在客户端有一个类似于你在服务器上的循环。如果你想每次都建立一个新的连接,你应该在服务器上优雅地关闭流并且没有这样的循环。如果消息较长或者您需要为命令指定最大长度,您仍然需要循环。

关于c# - TcpClient - 现有连接被远程主机强行关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24767184/

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