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

时间:2018-05-12 03:17:45

标签: c# tcp tcpclient tcplistener

所以我试图制作一个代码,其中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);

2 个答案:

答案 0 :(得分:1)

您无法从非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");
    }

答案 1 :(得分:0)

我认为我的缓冲部分错误,但我修复了它

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");
                }