C # socket accept byte byte length

I'm trying to learn how to use sockets in C #, and I doubt I use code like this:

byte[] data = new byte[64]; int length = 0; length = sock.Receive(data); //more code... 

So, the byte[] data is filled with the received data, and the left space in the array is filled with 0s, is byte[] completely allocated to the memory (all 64 bytes)? If so, is there a way to make byte[] the same size as the actual data sent?

+5
source share
3 answers

You can check sock.Available to see what has already happened. (until)

 byte[] data = new byte[sock.Available]; int length = sock.Receive(data); //more code... 

Note. Since you may or may not know what will happen next in the network, it usually makes sense to first read only the header (with size information) or allocate more space than necessary and call .Recieve() multiple until it is reached end of record.

Note. This code assumes that you already know that there is some data to receive, and you have been waiting long enough for some useful amount of data to be ready.

If you prefer to use length headers, .Available can help you avoid reading the partial header and having to reassemble it, which is nice. (Only large posts may need manual assembly in this case)

+3
source

As noted, usually reading can return fewer bytes, then it was said. See the Workaround below, which ensures that it reads as many bytes as it was said - basically the size of the buffer passed. Function from here .

 /// Reads data into a complete array, throwing an EndOfStreamException /// if the stream runs out of data first, or if an IOException /// naturally occurs. /// </summary> /// <param name="stream">The stream to read data from</param> /// <param name="data">The array to read bytes into. The array /// will be completely filled from the stream, so an appropriate /// size must be given.</param> public static void ReadWholeArray (Stream stream, byte[] data) { int offset=0; int remaining = data.Length; while (remaining > 0) { int read = stream.Read(data, offset, remaining); if (read <= 0) throw new EndOfStreamException (String.Format("End of stream reached with {0} bytes left to read", remaining)); remaining -= read; offset += read; } } 

First you can use this method to read: 2 byte integer , which should represent the number of bytes that will follow. Then you read again, but now read as many bytes as indicated in this double-byte whole.

But for this to work, it is clear that the sender must first send an integer of two bytes, which is the length of the data that will follow, and then the data itself.

So basically you call the function above in a byte array of size two first (to get the length of the data), and then in the byte array with the size specified in that 2-byte integer (to get the data).

You can use this to read from NetworkStream . A few more readings on this topic.

+2
source

You just need to use the return value from Receive to understand how much data is received. You can shorten the buffer using Array.Resize if you want, but this will usually be a sign that something is wrong.

Also note that TCP is a byte stream and does not preserve message boundaries.

+1
source

Source: https://habr.com/ru/post/1238309/


All Articles