vb.net - Byte initialized reading socket in loop -
i'm newbi , trying write client code receives data server , output textbox. problem when 2nd serverstream.read(instream, 0, instream.length) in loop initializes instream values.
private sub button1_click(sender object, e eventargs) handles button1.click serverstream.read(instream, 0, 5) receiveddata = encoding.ascii.getstring(instream) serverstream.flush() if (string.compare(receiveddata, "_ok_") = 0) msg(receiveddata) dim recv = serverstream.read(instream, 0, instream.length) receiveddata = encoding.getencoding("windows-1252").getstring(instream, 0, recv) msg(receiveddata) if (string.compare(receiveddata, "end") = 0) msg("server closed") end if loop while serverstream.dataavailable else msg("unable recieve msg") end if end sub
i'm bushing around web sites still don't understand i've done wrong...
in first call read
:
serverstream.read(instream, 0, 5)
you ignore it's return value. might have read little 1 byte. better second usage (reading in loop), again seem assuming you'll receive entire "messages" (e.g. end
) sent other end of connection.
there no such guarantees. need loop (and keep track of in buffer start writing newly received data) until detect (somehow) you've received complete "messages". done by:
- prefixing message length, or
- by using byte value cannot appear within messages end-of-message marker, or
- sending fixed-length messages, or
- including message "type" code @ start of message, each message type if fixed length
Comments
Post a Comment