0

So I was trying to make a code where TCP will accept all client which involves the repeat accept client but when I use that, I cant receive any response from the client so how do I solve this?

The messagebox won't even pop up on the server

code for the buffer location (Background worker):

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

Code where ill show messagebox when client send a response back to the server (Background worker):

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

Here is part of the code where client receives a response from server then send a server a message (Part of the code but mainly focus on this):

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 Answers 2

1

You can't update the UI from a non-UI thread (in this case a background worker).

Change:

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

...to:

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");
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help but for some reason, this did not work
I have edit my question with the new code that you supplied me. So is there any error in the code? Tho i also did enable reportprocess
@zhiyan114 well naturally you need to apply the pattern above to your other workers. Sorry I should have made that clear. See how you go
Just quick one update on your answer, i actually got it fixed and yes, messagebox actually does show when it use in background worker.
@zhiyan114 post your claim as an answer for all to see
0

I think my buffer part error but i fixed it

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.