views:

2123

answers:

5

Hi to all,

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.

+5  A: 

as the error tells use byte[]

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

See also this

PoweRoy
thanks for the answer, the page would help me
Cmptrb
+1  A: 

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
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
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.
Timothy Walters
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
A: 

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

  deviceSocket.Receive(coming.ToArray());
Are you sure it works? It compiles, but do you get the data?
Henk Holterman
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
Cmptrb
A: 
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
+1  A: 

You have a bug in your code. The moment you do List.ToArray(), it creates a new array object. So, the socket is putting data into this new array, and not in the original List that you have.

We could help better if we understood exactly what the messages are, and what their formats are? If you have control over your device, you could have it send each message preceded by an integer which tells how many bytes are coming in data. Then you could issue a read for exactly that many data bytes...

// allocate a data buffer for some max length.
byte [] buffer = new byte[1024];
// issue a read for 4 bytes, which is the header that signifies
// the length of data coming later.
int read = Socket.Receive(buffer, 0, 4);
int dataSize = BitConverter.GetInt(buffer, 0, 4);
// now issue a read for dataSize #of bytes
read = Socket.Receive(buffer, 0, dataSize);

I havent tried this code snippet, but I think you could get this to work. And you could add more metadata about the packet after the header, for eg, add the CRC etc.

On my blog, I wrote a series of articles on how to implement PING with Sockets. In that you could get some idea on how to structure your packets.

http://ferozedaud.blogspot.com/2009/09/implementing-traceroute-with-systemnet.html

Hope this helps. Good luck!

feroze