c# - Parse the received message from the server -
the problem i'm having parsing bytes i'm reading through server client's interface , show them message received.
the general idea send message, message goes server , send message clients connected chatroom.
but happens when enter chat room message appears ">>ignusername" mark new user has entered chat.
and when try send message such "hello" server receives messages correctly when it's time send message users appears on screen ">>ignusername". original message. i'm not sure faulty extract of code fix issue i'm having.
conn.server.beginreceive(dat, 0, 1024, socketflags.none, new asynccallback(recibiendof), so); if (//something has been received) { mensajenuevo = msjerecibidof(dat); }
this callback
public void recibiendof(iasyncresult ar) { try { stateobject = (stateobject)ar.asyncstate; conn.server = so.worksocket; int bytesread = conn.server.endreceive(ar); if (bytesread > 0) { string mensajote = system.text.encoding.ascii.getstring(mensajenuevo); if (textbox_curmsg.invokerequired == true) { this.textbox_curmsg.begininvoke((methodinvoker)delegate { textbox_curmsg.text = mensajote; }); } else textbox_curmsg.text = mensajote; //recibemsje(); } else { if (so.sb.length > 1) { string response = so.sb.tostring(); } //recibemsje(); } } catch { } }
and function wanted use before using callback
public byte[] msjerecibidof(byte[] msej) { byte[] mensajeiro = new byte[1024]; mensajeiro = msej; mensajenuevo = msej; return mensajeiro; }
conn.server.beginreceive(dat, 0, 1024, socketflags.none, new asynccallback(recibiendof), so); if (//something has been received) { mensajenuevo = msjerecibidof(dat); }
this not how work. using asyn receive method returns immediately. callback called when actual receive happens. method msjerecibidof(dat);
passed empty dat
or , older data.
also, @ end of public void recibiendof(iasyncresult ar)
have call onn.server.beginreceive
again receive next message.
because have posted part of codes, best can answer. fix these , see...
Comments
Post a Comment