Is there any way to make sure that there is enough data to fill the buffer or have the receive function wait until the is enough data?
My current code doesn't work properly if there isn't enough data to fill the buffer:
[code]byte[] buffer = new byte[1024];
socket.Receive(buffer);[/code]
Did you set an explicit timeout value for your socket? If not, Socket.Receive should block (wait) until it has received enough data to fill the provided buffer.
Are you using a connectionless protocol or is it a stream protocol like TCP?
I have not set a timeout value and I am using TCP.
[QUOTE=IpHa;22349513]I have not set a timeout value and I am using TCP.[/QUOTE]
How does Recieve behave then? It shouldn't return until it has read 1024 bytes or encountered an error. Check the return value.
The first two times it reads 1027 bytes(the correct amount), but the third time it only reads 862 bytes.
[QUOTE=IpHa;22351972]The first two times it reads 1027 bytes(the correct amount), but the third time it only reads 862 bytes.[/QUOTE]
Is there a setting to make it block?
Yes, and it is enabled by default.
[editline]08:49PM[/editline]
I have a workaround for now, but I know there has to be a better way to do it:
[code]while (serverSocket.Available < 1027)
{
Thread.Sleep(10);
}
byte[] buffer = new byte[1027];
serverSocket.Receive(buffer);[/code]
Are you sure that the last time the remote socket hadn't closed after sending those 862 bytes? Because that would cause Receive to return with the data it currently has.
It's definitely still open. I can still receive data, the next read just starts when the last was cut short.
[url]http://msdn.microsoft.com/en-us/library/8s4y8aff.aspx[/url]
[quote=msdn]If [B]no data[/B] is available for reading, the Receive method [B]will block until data is available[/B], unless a time-out value was set by using Socket.ReceiveTimeout.
If you are using a connection-oriented Socket, the Receive method [B]will read as much data as is available[/B], up to the size of the buffer.[/quote]
This is normal behaviour. It returns how much was read, and you should use that in your next call to specify how much more to read until all bytes are read. (Offset = Read, Size = 1024 - Read)
[url]http://msdn.microsoft.com/en-us/library/w3xtz6a5.aspx[/url]
[QUOTE=IpHa;22352225]Yes, and it is enabled by default.
[editline]08:49PM[/editline]
I have a workaround for now, but I know there has to be a better way to do it:
[code]while (serverSocket.Available < 1027)
{
Thread.Sleep(10);
}
byte[] buffer = new byte[1027];
serverSocket.Receive(buffer);[/code][/QUOTE]
Network sockets aren't instant - when you call socket.Write(500 bytes) from one location, it will take a few moments to arrive on your destination, and the packets you receive may be split up in to chunks which you need to re-assemble.
What this means is that if you send:
socket.Write("Hello there");
It could very well appear like this on your other computer which calls socket.Receive():
"Hell"
"o ther"
"e"
Each line above is a call to Receive().
So what you really want to do is:
* Use a buffer size of your choosing, eg 1024 and keep Receive()ing in a loop (and block until you get data)
* Block on socket.Receive() and keep appending the data you receive to your own bigger buffer
* Have an event in your program that knows when a packet has finished (so you can then process it), or just close the socket from the remote side when you're finished to indicate that. For example, if you're doing a chat program, you may end all 'packets' with a new line character. When you see this new line character in your received data, you know you need to process all of the data you've received so far up to this line character as a command.
I just had a quick look for some tutorials on C# chat servers, but could not find a decent one!
Maybe you would have some better like. But you may want to check those out, it should help you understand how to go forward in your app.
Sorry, you need to Log In to post a reply to this thread.