在 C# 中使用 TCP 从客户端错误接收多个响应

Receive multiple response from client error using TCP in C#

所以我试图制作一个代码,其中 TCP 将接受所有涉及重复接受客户端的客户端,但是当我使用它时,我无法收到来自客户端的任何响应,所以我该如何解决这个问题?

消息框甚至不会在服务器上弹出

缓冲区位置代码(后台工作者):

while (true)
{
            try
            {
                client = network.AcceptTcpClient();
                streamer = client.GetStream();
                buffer = new byte[client.ReceiveBufferSize];
            } catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Client Connection Error");
                client.Close();
                streamer.Close();
                Close();
            }
        }

当客户端向服务器(后台工作人员)发送响应时显示消息框的代码:

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        while (true)
        {
            string data = Encoding.Unicode.GetString(buffer, 0, 
streamer.Read(buffer, 0, client.ReceiveBufferSize));
            if (data == "Response_Command_329873123709123")
            {
                int slowdown = streamer.Read(buffer, 0, 
client.ReceiveBufferSize);
                var message = Encoding.Unicode.GetString(buffer, 
0,slowdown);
                worker.ReportProgress(0, message);
            }
        }
    }
    private void backgroundWorker2_ProgressChanged(object sender, 
 ProgressChangedEventArgs e)
    {
        MessageBox.Show((string)e.UserState, "Client Response");
    }

这是客户端从服务器接收响应然后向服务器发送消息的部分代码(部分代码但主要关注此):

else if (data == "Messagebox_Command_2837190092703817203")
            {
                int txtbox = streamer.Read(buffer, 0, 
client.ReceiveBufferSize);
                string txt = Encoding.Unicode.GetString(buffer, 0, txtbox);
                MessageBox.Show(txt, "Message From Server");
                byte[] database = 
Encoding.Unicode.GetBytes("Response_Command_329873123709123");
                streamer.Write(database,0,database.Length);
                byte[] databases = Encoding.Unicode.GetBytes("test");
                streamer.Write(databases, 0, databases.Length);

您不能从非 UI 线程(在本例中为后台工作者)更新 UI。

变化:

while(true)
        {
            string data = Encoding.Unicode.GetString(buffer, 0, 
streamer.Read(buffer, 0, client.ReceiveBufferSize));
            if(data == "Response_Command_329873123709123")
            {
                MessageBox.Show(Encoding.Unicode.GetString(buffer, 0, 
streamer.Read(buffer, 0, client.ReceiveBufferSize)), "Client Response");
            }
        }

...至:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

    while(true)
    {
        string data = Encoding.Unicode.GetString(buffer, 0, 
                       streamer.Read(buffer, 0, client.ReceiveBufferSize));
        if(data == "Response_Command_329873123709123")
        {
            var message = Encoding.Unicode.GetString(buffer, 0, 
                                   streamer.Read(buffer, 0, client.ReceiveBufferSize));

            worker.ReportProgress (0,  message);
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        MessageBox.Show((string)e.UserState, "Client Response");
    }

我认为我的缓冲部分有误,但我已修复它

int databases = streamer.Read(buffers, 0, buffers.Length);
                string data = Encoding.Unicode.GetString(buffers, 0, databases);
                if (data == "Response_Command_329873123709123")
                {
                    byte[] datacen = new byte[client.ReceiveBufferSize];
                    int main = streamer.Read(datacen, 0, datacen.Length);
                    var message = Encoding.Unicode.GetString(datacen, 0, main);
                    MessageBox.Show(message, "Client Response");
                }