.net - Socket.Receive does not block until all data is received -


ai!

i'm trying ftp file list , got small library did that. thing socket.receive() function not block until data received. when set breakpoint @ line receives data if don't there 38 bytes (should around 380).

here code:

while true     dim bytes integer = csocket.receive(buffer, buffer.length, socketflags.none)     mes += ascii.getstring(buffer, 0, bytes)     if bytes < buffer.length         exit while     end if end while 

as said: works if breakpoint set in line .receive part. use workaround guarantees 1 loop seems dirty. ideas?

//edit: let me add more information. don't know how data receive. code function receives data ftp server. thought stop when message ends crlf can't because there crlf between each file listed. example: file1.textcrlffile2.txtclrffile3.txtcrlf

sometimes when call receive returns 1 file crlf @ end if there more files listed. condition seems unstable me.

what mean "all data" in case? until socket closed, there can more data. network stack doesn't know or care data logically belongs together. gets chopped in ip packets of varying sizes (determined complex algorithms).

for receiving party, means data being received, in chunks. need keep calling receive until know have data need.

from msdn documentation (emphasis mine):

if using connection-oriented socket, receive method read data available, up to number of bytes specified size parameter.

the reason why see different behavior when setting breakpoint, because literally pausing application while sending party sending data. operating system still receive packets , buffer them, point. standard buffer size 8192 bytes , can changed receivebuffersize property.

code

how translate in code? assuming know how data need receive, improved code this:

dim bytesremaining integer = buffer.length dim sb new stringbuilder while bytesremaining > 0     dim bytes integer = csocket.receive(buffer,                                            bytesremaining, socketflags.none)     bytesremaining -= bytes     sb.append(ascii.getstring(buffer, 0, bytes)) end while mes = sb.tostring() 

the condition in loop gone, because don't want exit if don't receive lot of bytes, want keep reading. bytesremaining keeps running count of how many bytes still received.

not relevant question, i've replaced mes string concatenation stringbuilder. potentially creating lot of new strings, (except last) need garbage collected.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -