Sockets - receiving a large buffer?
(First time trying to do socket programming :) )
I'm trying to read a large buffer using sockets. Is there anything wrong with this psudo code?
Code:
DO
mySocket.Receive(buff) ' temporary buffer, discard the data
WHILE (mySocket.Available > 0)
it terminates after the first iteration because, I suppose, it reads the data faster than the sender is sending it, and Socket.Available will be 0 (if i break the debugger and step through it, it receives the whole buffer). How can i make the Receive call block until more data is available? (I've tried setting Socket.ReceiveTimeout, but it doesn't seem to do any good)
Re: Sockets - receiving a large buffer?
The Receive method will read all data available. If no data is available it will block until data is available.
It all boils down to what it is you're reading from the stream and why. If you need to be reading from the stream continously, and "endless" loop is the normal approach:
Code:
Do
mySocket.Receive(buff)
' Do some more work with the received data
While True
Altough if that is not the case, you would have to know when you have received all of it. Since you're the only one that knows what it is you're receiving, do you have anything that could tell you when you've received all data?
Re: Sockets - receiving a large buffer?
no.... no indication of when i've received the whole buffer
see, on the server side I'm sending the data in small buffers (using unix sockets), so a call to Receive from the .NET client app might return after reading that chunk of data, and think that no more data is available.....
so is there no elegant way of making the Receive() call wait until more data is available? if not i guess I'll stick with the infinite loop :)
Re: Sockets - receiving a large buffer?
Quote:
Originally Posted by
MrPolite
..
so is there no elegant way of making the Receive() call wait until more data is available? ...
Well...thats actually what Receive does if you call it when no more data is available. If on the other hand, data is available when you call Receive, it'll return that data to you. What I believe you're asking for would require the socket to know that more data is on its way (and wait for it to arrive, THEN return the data to you), which it cant:)