Dynamic Byte Array in C# by Socket Programming [List<byte> does not work]

25,987

Solution 1

as the error tells use byte[]

Socket deviceSocket = new Socket(server);
byte[] coming = new byte[buffersize];
...
deviceSocket.Receive(coming)

See also this

Solution 2

I would solve it like this:

int bytesRead = 0;
byte[] incomming = new byte[1024];
byte[] trimmed;

try
{
    bytesRead = sTcp.Read(incomming , 0, 1024);
    trimmed = new byte[bytesRead];

    Array.Copy(incomming , trimmed , bytesRead);
}
catch
{
    return;
}

but a small reminder is that you actually creates 2 arrays, thus using more memory!

Solution 3

The Socket.Receive() method will fill a buffer with as much data as it can fit, or as much data is available, whichever is lower.

If you know all your messages are under 2048 bytes then you could declare your buffer as follows:

byte[] buffer = new byte[2048];
int bytesReceived = 0;
// ... somewhere later, getting data from client ...
bytesReceived = deviceSocket.Receive( buffer );
Debug.WriteLine( String.Format( "{0} bytes received", bytesReceived ) );
// now process the 'bytesReceived' bytes in the buffer
for( int i = 0; i < bytesReceived; i++ )
{
    Debug.WriteLine( buffer[i] );
}

Of course you probably want to do something more than write the bytes to the debug output, but you get the idea :)

You still need to be aware that you may get incomplete data, if the client broke the message into multiple packets then one might come through (and be received) and then later another. It's always good to have some way of telling the server how much data to expect, then it can assemble the complete message before processing it.

Share:
25,987
Cmptrb
Author by

Cmptrb

Updated on December 14, 2021

Comments

  • Cmptrb
    Cmptrb over 2 years

    I'm sending to a device a request as byte array and I want to receive the anwser device gives.

    ...
    Socket deviceSocket = new Socket(server);
    List<byte> coming = new List<byte>();
    ...
    deviceSocket.Receive(coming)
    

    Here the program gives error: Error 1
    The best overloaded method match for 'System.Net.Sockets.Socket.Receive(byte[])' has some invalid arguments Error 2
    Argument '1': cannot convert from 'System.Collections.Generic.List' to 'byte[]'

    How can I solve it ?

    Thanks.

  • Cmptrb
    Cmptrb over 14 years
    the client [a device at my project] sends for different request answers at different lengths. I receive data with its CRC-bytes so I can understand that if the answer is corrupted. thanks for the answer, I got the idea :)
  • Timothy Walters
    Timothy Walters over 14 years
    Do your messages include a known 'terminator'? e.g. \0x00, if so then you can keep recieving into your buffer, and process the contents of the buffer into a List<byte> until you get this terminator, anything left in the buffer is part of another message.
  • Cmptrb
    Cmptrb over 14 years
    no, my receiving messages do not include any terminators, and they have different length and i need really a dynamic byte array to my work. Finally the worse case is to set an static array with a high length. This would harmful for me, because I should write an analyse algorithm for coming messages. I do only know the length of answer of my requests ...
  • Bhavesh
    Bhavesh over 2 years
    Please add bit more detail. e.g what is 'coming' and what you trying to do inside foreach.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.