C # dynamic socket byte array [List <byte> not working]

I am sending a request to the device as a byte array, and I want the anwser device to receive it.

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

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

How can i solve this?

Thanks.

+3
source share
5 answers

, Receive, :

  deviceSocket.Receive(coming.ToArray());
+1

[]

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

. this

+6

Socket.Receive() , , , , .

, 2048 , :

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] );
}

, , , - , debug, :)

, , , ( ), . , , .

+1

:

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;
}

, 2 , !

+1
byte[] coming = new byte[8];
deviceSocket.Receive(coming);
for (int i = 0; i < 8; i++)
{
    xtxtComing.Text += coming[i].ToString() + " ";
}

[xtxtComing !

.

                    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() + " ";
                    }

, xtxtComing . , , , < > .

, :)

0

Source: https://habr.com/ru/post/1716664/


All Articles