Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an app with a TCP Listener, and a remote client constantly sending data (once per second).
Using Hercules Setup utility as a server I am able to receive the data, but when I use my own application the data isn't displayed in the textBox.
All I can see in my app is that the length is 266- which is what I expect, but the data is just expressed as 2 rectangle boxes.

What could be the issue here?
public void bgWorkerSocketListener_DoWork(object sender, DoWorkEventArgs e)
        AppendTextBox2("Waiting for a connection... " + Environment.NewLine);

        server = new TcpListener(localAddr, port);


        // Buffer for reading data
        Byte[] bytes = new Byte[4096];
        String data = null;

        // Enter the listening loop.
        while (true)
            client = server.AcceptTcpClient();
            AppendTextBox2("Connected!" + Environment.NewLine);

            data = null;

            // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();

            int i;

            // Loop to receive all the data sent by the client.
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                // Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                AppendTextBox2("Length: " + data.Length + " Received: " + data);
    catch (SocketException ex)
        MessageBox.Show("SocketException: " + ex);
AppendTextBox2 is just an invoke method for appending my text box from another thread:
public void AppendTextBox2(string value)
    if (InvokeRequired)
        this.Invoke(new Action<string>(AppendTextBox2), new object[] { value });
    tBoxTcpIpStatus.Text += value;
    //move the caret to the end of the text
    tBoxTcpIpStatus.SelectionStart = tBoxTcpIpStatus.TextLength;

What I have tried:

Any idea what could be happening here?
It's not my append method, I already tried to remove it, without success.
Updated 24-Feb-21 6:06am
Richard MacCutchan 24-Feb-21 12:00pm    
The data is most likely not ASCCII. Talk to the people who are sending it. Alternatively use your debugger to examine what you are receiving.

1 solution

Start by finding out exactly what you are receiving: instead of using GetString to convert the bytes, convert them to a hex string (BitConverter.ToString Method (System) | Microsoft Docs[^] will do it) and add that to your text box.
You can then use an ASCII table to work out exactly what the client is sending you: ASCII - Simple English Wikipedia, the free encyclopedia[^]

Until you have actual info on what you are getting, you can't even begin to work out what the problem might be!

But as a suggestion, instead of using Invoke, us the BackgroundWorker's Progress reporting mechanism to display your data - you can pass any object you like via the UserState property of the ProgressChangedEventArgs in the ProgressChanged event handler.
Share this answer
Member 14827175 24-Feb-21 13:56pm    
Thank you so much, that was very helpful. I'll look into Progress reporting, it looks interesting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900