6

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
  • 255
  • 5
  • 8
  • 16

6 Answers6

6

as the error tells use byte[]

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

See also this

RvdK
  • 19,580
  • 4
  • 64
  • 107
1

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.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • 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 :) – Cmptrb Sep 02 '09 at 08:51
  • 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 until you get this terminator, anything left in the buffer is part of another message. – Timothy Walters Sep 03 '09 at 01:06
  • 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 ... – Cmptrb Sep 03 '09 at 11:57
1

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!

Darren
  • 68,902
  • 24
  • 138
  • 144
Crollum
  • 11
  • 1
0

If you require coming to act as a list prior to calling Receive you can also use:

  deviceSocket.Receive(coming.ToArray());
  • Good point, I may have rushed into answering and didn't look at why the Recieve method took an array in the first place. This solution will create a new array with the same contents as the list but which you will not be able to access later. The best solution would be to create a temporary array or use an array initially as in PoweRoy's answer. –  Sep 02 '09 at 09:06
  • it does work sorry, coming.ToArray() must be assigned to antoher array – Cmptrb Sep 03 '09 at 12:28
0
byte[] coming = new byte[8];
deviceSocket.Receive(coming);
for (int i = 0; i < 8; i++)
{
    xtxtComing.Text += coming[i].ToString() + " ";
}

the code above works in my listening loop [xtxtComing is a textbox !

List coming does not give any error by complying.

                    List<byte> coming1 = new List<byte>();
                    deviceSocket.Receive(coming1.ToArray());
                    for (int i = 0; i < coming1.ToArray().Length; i++)
                    {
                        xtxtComing.Text += "a " + coming1[i].ToString() + " ";
                    }

These code above in the same loop does not work, I can not get anything in xtxtComing textbox. Maybe I have a syntax error or as I believe Receive function do not work with List<> compatible.

Sorry for late answer, I have tried to get them :)

Cmptrb
  • 255
  • 5
  • 8
  • 16
0

Try this:

foreach (item in coming)
{
    xtxtComing.Text += $"a {item} ";
}
joeV
  • 1
  • Please add bit more detail. e.g what is 'coming' and what you trying to do inside foreach. – Bhavesh Dec 13 '21 at 23:26
  • 1
    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](/help/how-to-answer). – Community Dec 13 '21 at 23:27