c# - Asynchronous socket, receive string messages -


good evening, sorry in advance writing don't know error is...

my client application receiving server asynchronously. want transfer bunch of stuff @ once (the contents of array, couple of hundred bytes).

i want server able send "commands", , have function on client side act according these commands, example if message server reads "print_hello", should call function prints hello.

now, it's understanding when receiving data asynchronously, can't know how of data has been sent (or if more data expected has been sent), need store data in buffer, , when "end of command" (for example, '!') sign has been received, should know call function.

so far makes sense me, i'm having trouble implementing it. in datareceived callback function, have code:

console.writeline("raw data: {0}", data)); mainbuffer += data; console.writeline(mainbuffer); 

mainbuffer declared volatile static string mainbuffer = ""; first line prints correctly, , goes through of data expected. however, when print out mainbuffer, prints out first set of data receieved, rest not added buffer.

what cause this? thread safety issues? not reading latest value of mainbuffer? can't use breakpoints debug this.

sample output:

raw data: abc abc raw data: def abc raw data: ghi abc 

small update, tried using volatile static int well, , increments , prints correctly after each datareceived(). string still not updated however.

here problem "with messing code lines!":

//of course problem has noting string being volatile... private volatile string mainbuffer = string.empty;  byte[] buffer = new byte[1024];  while (networkstream.read(buffer, 0, buffer.length) > 0) {     string data = system.text.encoding.utf8.getstring(buffer);     console.writeline("raw data: {0}", data));     mainbuffer += data;     console.writeline(mainbuffer); } 

naturally output of code mentioned previously. here happening:

the string class in c# array of char start pointer first char in array , ends special "terminal" char \0.
when create byte array of n index, fill indexes of array default value of byte 0. 0 equals terminal char \0

byte b = (byte)`\0`;\\the value of b 0 

so, when call read(buffer), method not trim buffer fit data read. if buffer size "here 1024" larger data read, remaining bytes of buffer equals terminal char '\0', array of chars of generated string abc\0\0\0\0... index 1024. when add string def add @ last index of char array "after last \0", char array abc\0\0\0\0...def, because of def added after terminal char(s) console.write ignore after first \0!.

also note while debugging, if point mouse mainbuffer variable, see actual data contains maybe abc\0\0\0\0..def\0\0\0\0..ghi

however fix problem , generate reliable string, actual bytes read , generate string it. so:

int dataread = 0;  while ((dataread = networkstream.read(buffer, 0, buffer.length)) > 0) {     list<byte> actualbuffer = (new list<byte>(buffer)).getrange(0, dataread);      string data = system.text.encoding.utf8.getstring(actualbuffer.toarray());     console.writeline("raw data: {0}", data));     mainbuffer += data;     console.writeline(mainbuffer); } 

it wroth mention here should consider using stringbuilder instead of string.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -