multithreading - Cannot Append to Received String in UDP Listener C# -


i have form create udp object, in udp class udpclient created , received data done in beginreceive method using endreceive.

when print string of reveived data, after converting byte[], console within beginreceive method, text appended, received data prints not appended text.

so looks received data incomplete.

when string prints, newline , appended "done" not shown.

any great!!

thanks

class udp {     public eventhandler _datareceived;      public udp()     {          int receiverport = 1248;         udpclient receiver = new udpclient(receiverport);          string discovery = "<?xml version=\"1.0\"?><servicequery></servicequery>";          receiver.beginreceive(new asynccallback( datareceived), receiver);          ipendpoint end = new ipendpoint(ipaddress.broadcast, 1248);         receiver.send(encoding.ascii.getbytes(discovery + "\0"), discovery.length + 1, end); }  private void datareceived(iasyncresult ar) {     udpclient c = (udpclient)ar.asyncstate;     ipendpoint receivedipendpoint = new ipendpoint(ipaddress.any, 1248);      byte[] receivedbytes = c.endreceive(ar, ref receivedipendpoint);      string receivedtext = asciiencoding.ascii.getstring(receivedbytes);      console.writeline("\n");      if(_datareceived != null)     {         console.write(receivedipendpoint + ": " + receivedtext + environment.newline + "done");        _datareceived(receivedtext, new eventargs());     }      c.beginreceive(new asynccallback(datareceived), c); }      }     

the simplest repro can think of problem code:

    private void button1_click(object sender, eventargs e) {         byte[] receivedbytes = new byte[] { 0x48, 0x65, 0x6c, 0x00, 0x6c, 0x6f };         string receivedtext = encoding.ascii.getstring(receivedbytes);         console.write(receivedtext + ", won't see this");     } 

output after clicking button several times:

   helhelhelhel 

surely recognize poison-pill in receivedbytes array, presence of 0x00 byte causes output string cut short. nothing beyond byte makes visual studio output window.

explaining behavior requires pretty deep dive in how console.write() in winforms app works , how capable of generating output, though program has no console. long-winded story isn't entertain i'll punt short version. visual studio hosting process debugger option enabled, console.write() equivalent debug.write(). debug output intercepted defaulttracelistener class, pinvokes outputdebugstring() text appear in debugger trace window. these winapi functions takes c strings, c string zero-terminated indicate end of string.

there several ways fix this, programmer's way convert byte[] array content hex:

    byte[] receivedbytes = new byte[] { 0x48, 0x65, 0x6c, 0x00, 0x6c, 0x6f };     string receivedtext = bitconverter.tostring(receivedbytes);     console.writeline(receivedtext + ", see this"); 

output:

    48-65-6c-00-6c-6f, see     48-65-6c-00-6c-6f, see     48-65-6c-00-6c-6f, see 

or might want take better @ data transmit, ensuring printable text can converted encoding.ascii


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -