2

I'm trying to create a windows application with Visual Studio 2012 but weird stuff seems to be happening... When I run the exact same code in a console app, it works fine but it seems that I can't output the following once I run it in a thread on a windows application project:

private void VisualUDPListener_Load(object sender, EventArgs e)
    {
        //System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
        new Thread(delegate()
        {
            StartListener();
        }).Start();
    }

    private void StartListener()
    {

        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

        try
        {
            while (true)
            {
                //log text box
                Log.AppendText("Listening \n");

                byte[] bytes = listener.Receive(ref groupEP);

                string hex_string = BitConverter.ToString(bytes);//this works and returns the correct hex data
                string ascii_string = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//blank???????????

                MessageBox.Show(ascii_string.Length.toString());//outputs 131 which is correct

                MessageBox.Show(ascii_string);// shows a blank message box
                Log.AppendText("--" + ascii_string + hex_string +" \n");//only outputs --
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }

I target .NET Framework 4.5... If I send UDP data from a test java app it receives the data fine but if I send the exact same data through a mobile device which the code is intended for it comes out empty like in the comments above. (Then the device must be sending corrupt data? nope because if the code above is run in a console app it runs perfectly and outputs the correct strings)

Any help would be greatly appreciated.

Raffikki
  • 23
  • 5
  • 4
    Does the data contain a byte of 0 at any point? – Jon Skeet Jan 02 '13 at 22:07
  • How does hex_string look like in case when Encoding.ASCII.GetString works and doesn't? – Sergey Zyuzin Jan 02 '13 at 22:07
  • Why not Encoding.ASCII.GetString(bytes); ??? – scartag Jan 02 '13 at 22:08
  • Tried to reproduce the issue, and I couldn't get the MessageBox to show, which makes sense because you not calling `MessageBox.Show` from the main UI thread ( http://stackoverflow.com/questions/559252/does-messagebox-show-automatically-marshall-to-the-ui-thread ). Also, `MessageBox.Show(ascii_string.Length)` does not compile (method expects a string). Are you sure you posted the code you are testing with? – Diego Jan 02 '13 at 22:28
  • The byte array isn't empty as it returns the hex string properly. The hex string contains: 00-04-02-00-20, it looks exactly the same...it works inside a console app but not windows) – Raffikki Jan 02 '13 at 22:38
  • Sorry about that: MessageBox.Show(ascii_string.Length.ToString()); – Raffikki Jan 02 '13 at 22:42
  • 1
    It looks like it always works. But MessageBox invokes windows API, which treats 0 as line end, so the string seems empty to windows API. – Sergey Zyuzin Jan 02 '13 at 22:46
  • @user671280: Post that as an answer (provide a little more detail, including the fact that the OP has stated himself that the data starts with a 0), and I'll upvote it. – Robert Harvey Jan 02 '13 at 22:49
  • I can't output the data at all, even if I try to log or append it to a textbox, will the starting 0 affect that as well? If so how do I remove it? – Raffikki Jan 02 '13 at 22:53

1 Answers1

3

As noted in comments, the string starts with 0 byte (00-04-02-00-20). This correctly gets converted to C# string. MessageBox.Show invokes windows api function MessageBox. Windows API uses zero-terminated strings, so this specific string appears empty to WinAPI, because first byte is zero. You cannot log/display this string verbatim with APIs that use zero-terminated strings.

You need either to replace 0s with something else, like ascii_string.Replace((char)0, (char)1) or use apis which don't treat zeros as special characters.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Sergey Zyuzin
  • 3,754
  • 1
  • 24
  • 17