Problem
I have PHP client which sends a image file to a C# socket server. My problem is about 30% of the time the file is partially transferred and stops.
PHP END ->
$file = file_get_contents('a.bmp');
socket_write($socket,$file);
C# END ->
int l= Socket.Receive(buffer, buffer.Length, SocketFlags.None);
//create the file using a file stream
How can I always transfer the full file without intermediate states? And why does it happen?
From the documentation for Socket.Receive:
This means you may get less than the total amount. This is just the way sockets work.
So if if you get a partial read, you should call
Socket.Receiveagain. You can use the overload ofSocket.Receiveto continue reading into the same buffer.Here is an article that shows how “keep reading” until you get what you want:
If you don’t know how big the data is, you must keep reading until
Socket.Receivereturns zero.