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.

6 Answers 6

6

as the error tells use byte[]

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

See also this

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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 :)
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.
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 ...
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!

Comments

0

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

  deviceSocket.Receive(coming.ToArray());

2 Comments

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.
it does work sorry, coming.ToArray() must be assigned to antoher array
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 :)

Comments

0

Try this:

foreach (item in coming)
{
    xtxtComing.Text += $"a {item} ";
}

2 Comments

Please add bit more detail. e.g what is 'coming' and what you trying to do inside foreach.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.