Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# socket receive byte array length

Tags:

c#

bytearray

i'm trying to learn to use sockets in c# and i've a doubt, i'm using a code something like this:

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

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

like image 987
Pau C Avatar asked Dec 15 '15 18:12

Pau C


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You can check sock.Available to see what has already come in. (so far)

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

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

Note: This code assumes you already know there is some data to receive and you've waited long enough for some useful amount of data to be ready.

If you do choose to use length headers, .Available can help you avoid reading a partial header and having to re-assemble it which is nice. (Only large messages may need manual reassembly in that case)

like image 122
ebyrob Avatar answered Sep 18 '22 09:09

ebyrob


You simply need to use the return value from Receive to understand how much data has arrived. You can shorten the buffer using Array.Resize if you want but that normally would be a sign that something is wrong.

Also note, that TCP is a stream of bytes and does not preserve message boundaries.

like image 21
usr Avatar answered Sep 17 '22 09:09

usr