通过套接字编程在 C# 中实现动态字节数组 [List<byte> 不起作用]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1366452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic Byte Array in C# by Socket Programming [List<byte> does not work]
提问by Cmptrb
I'm sending to a device a request as byte array and I want to receive the anwser device gives.
我将请求作为字节数组发送到设备,我想接收 anwser 设备提供的。
...
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[]'
此处程序给出错误:错误 1
'System.Net.Sockets.Socket.Receive(byte[])' 的最佳重载方法匹配有一些无效参数错误 2
参数 '1':无法从 'System.Collections.Generic 转换.List' 到 'byte[]'
How can I solve it ?
我该如何解决?
Thanks.
谢谢。
采纳答案by Timothy Walters
If you require coming to act as a list prior to calling Receive you can also use:
如果您需要在调用 Receive 之前充当列表,您还可以使用:
deviceSocket.Receive(coming.ToArray());
回答by RvdK
回答by Timothy Walters
The Socket.Receive() method will fill a bufferwith as much data as it can fit, or as much data is available, whichever is lower.
Socket.Receive() 方法将用尽可能多的数据填充缓冲区,或者尽可能多的可用数据,以较低者为准。
If you know all your messages are under 2048 bytes then you could declare your buffer as follows:
如果您知道所有消息都在 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] );
}
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.
您仍然需要注意,您可能会得到不完整的数据,如果客户端将消息分成多个数据包,那么一个数据包可能会通过(并被接收),然后再通过另一个数据包。有某种方式告诉服务器需要多少数据总是好的,然后它可以在处理之前组合完整的消息。
回答by Cmptrb
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 !
上面的代码在我的监听循环中有效 [xtxtComing 是一个文本框!
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.
上面在同一个循环中的这些代码不起作用,我无法在 xtxtComing 文本框中获取任何内容。也许我有语法错误,或者我认为 Receive 函数不能与 List<> 兼容。
Sorry for late answer, I have tried to get them :)
抱歉回复晚了,我试图让他们:)
回答by Crollum
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!
但一个小小的提醒是,您实际上创建了 2 个数组,从而使用了更多内存!