我们如何才能找到从套接字接收到的所有字节?
我想知道我们何时从没有 Socket.Disconnect() 的套接字接收所有字节 方法?
然而,我使用此代码来接收所有字节,但当 Scoket.Send(byte[]) 方法完成时,我使用 Socket.Disconnect() 方法。
List<byte> LBytes = new List<byte>();
do
{
System.Threading.Thread.Sleep(50);
BytesRead = obj_socket.Receive(obj_buffer, 0);
LBytes.Append(obj_buffer);
} while (BytesRead != 0);
因为当我们断开套接字时,套接字读取 0 字节。 例如我们发送了 100,000 字节,我们应该接收 100,000 字节。你怎么做到这一点?
I want to know when we receive all bytes from a socket without Socket.Disconnect()
method?
Yet , I use this code to receive all bytes , but i use Socket.Disconnect() method when Scoket.Send(byte[]) method is complete.
List<byte> LBytes = new List<byte>();
do
{
System.Threading.Thread.Sleep(50);
BytesRead = obj_socket.Receive(obj_buffer, 0);
LBytes.Append(obj_buffer);
} while (BytesRead != 0);
because when we disconnect the socket , socket reads 0 bytes .
for example we sent 100,000 bytes , and we should receive 100,000 bytes . How you do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然套接字是打开的,但没有办法做到这一点而不导致它在到达末尾时阻塞(期望更多数据)。
有几种方法可以做到这一点:
0
是常见的选择;发送任意二进制数据时很棘手,不过)如果它是一个
NetworkStream
,例如,使用长度前缀:While the socket is open, there is no way of doing this without causing it to block when it reaches the end (expecting more data).
There are several ways of doing it:
0
being a common choice; tricky when sending arbitrary binary data, though)If it was a
NetworkStream
, for example, using a length prefix: