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

rod*_*dit 5 c# tcplistener tcpclient

信息

我一直在用 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.
}
Run Code Online (Sandbox Code Playgroud)

注 - 服务器在与 Web 服务器和主控制台应用程序线程不同的线程上运行。

这是我的客户:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题

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

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
Run Code Online (Sandbox Code Playgroud)

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

这是描述问题的屏幕截图: 远程控制台和服务器通信和错误报告。

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

再次感谢

Sti*_*gar 5

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

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

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

  • @rodit“绿色勾号”(接受答案)不应该是对帮助您的人的奖励,如果他们也没有回答您提出的问题。 (7认同)