读入缓冲区 [英] reading into the buffer

查看:108
本文介绍了读入缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想以某种方式在内部缓冲区中排列字节。我

在套接字中接收这些字节。


一种解决方案是读入套接字:


byte [] buffer = new byte [65536];

int index = 0;

m_socket.Receive(buffer,8,SocketFlags.None);

索引+ = 8;

//读取消息的其余部分

// int msgLength是缓冲区中的剩余字节数

m_socket.Receive(缓冲区,索引,msgLen - 索引,

SocketFlags.None);

问题:


1 。如何计算msgLength?

2.如何在异步读取数据时进行此操作,

使用BeginReceive()


谢谢

Say I want to arrange bytes in the internal buffer in a certain way. I
receive those bytes in the socket.

One solution is to read in socket in pieces:

byte[] buffer = new byte[65536];
int index = 0;
m_socket.Receive(buffer , 8, SocketFlags.None);
index += 8;
//read the rest of the message
//int msgLength is remaining number of bytes in the buffer
m_socket.Receive(buffer , index, msgLen - index,
SocketFlags.None);
Questions:

1. How to calculate msgLength ?
2. How to make this operation when reading data in asynchronously,
using BeginReceive()

Thanks

推荐答案

2008年10月24日星期五10:08:42 -0700,puzzlecracker< ir ********* @ gmail.com>

写道:
On Fri, 24 Oct 2008 10:08:42 -0700, puzzlecracker <ir*********@gmail.com>
wrote:

说我想以某种方式安排内部缓冲区中的字节。我

在套接字中接收这些字节。


一种解决方案是读入套接字:


byte [] buffer = new byte [65536];

int index = 0;

m_socket.Receive(buffer,8,SocketFlags.None);

索引+ = 8;

//读取消息的其余部分

// int msgLength是缓冲区中的剩余字节数

m_socket.Receive(缓冲区,索引,msgLen - 索引,

SocketFlags.None);


问题:


1.如何计算msgLength?
Say I want to arrange bytes in the internal buffer in a certain way. I
receive those bytes in the socket.

One solution is to read in socket in pieces:

byte[] buffer = new byte[65536];
int index = 0;
m_socket.Receive(buffer , 8, SocketFlags.None);
index += 8;
//read the rest of the message
//int msgLength is remaining number of bytes in the buffer
m_socket.Receive(buffer , index, msgLen - index,
SocketFlags.None);
Questions:

1. How to calculate msgLength ?



如果通过缓冲区中剩余的字节数,你的意思是准备从套接字读取
,答案是你不要。出于同样的原因,

使用Poll()或Select()这样的方法无法提供任何关于

的保证当你真正尝试从套接字读取时会发生什么,没有

可靠的方法以

的方式计算套接字缓冲区中尚未读取的字节数,这将允许您在以后的调用中依赖于计算

接收()。


你能做的最好的事就是为Receive()提供一个缓冲区,这个缓冲区很大

够了对于给定消息你希望获得的剩余字节(我是

,假设前八个字节中的数据以某种方式允许你

来计算)。然后继续阅读循环,直到你已经读过

多个字节,减少你提供给Receive()的长度,每次

迭代考虑到已经读取的字节数。


也就是说,如果你不需要高效的i / o
,这只是你应该做的事情。

你的代码和网络缓冲区之间有很多开销来回,如果有很多数据来源

通过,你可以风强迫网络驱动程序必须抛弃

传入数据,并让另一端再次发送它。


更好的方法是网络i / o代码总是只读取大量数据,并让代码的另一层处理解析为可用消息的
。这样做实际上并不一定需要

直接解决上述问题(毕竟,网络i / o层是

可能仍然会以数据流形式传递数据),但确实将它变成了一个你可以控制更多的域。

If by "remaining number of bytes in the buffer", you mean ready to be read
from the socket, the answer is "you don''t". For the same reason that
using a method like Poll() or Select() cannot provide any guarantees about
what will happen when you actually try to read from the socket, there''s no
reliable way to calculate the bytes yet unread in the socket buffer in a
way that will allow you to depend on the calculation in a later call to
Receive().

The best you can do is provide a buffer to Receive() that is just large
enough for the remaining bytes you expect to get for a given message (I''m
assuming that the data in the first eight bytes in some way allow you to
calculate that). Then just keep reading in a loop until you''ve read that
many bytes, decreasing the length you offer to Receive() with each
iteration to take into account the number of bytes read already.

That said, this is only something you should be doing if you don''t need
efficient i/o. There''s a lot of overhead going back and forth between
your code and the network buffers, and if there''s a lot of data coming
through, you can wind up forcing the network driver to have to throw out
incoming data, and making the other end send it again.

A much better approach is for the network i/o code to always just read as
much data as it can, and let another layer of your code deal with parsing
that out into usable messages. Doing so won''t actually necessarily
address the above question directly (after all, the network i/o layer is
probably still going to be delivering data as a stream), but it does shift
it into a domain where you have a bit more control.


2.如何进行此操作异步读取数据,

使用BeginReceive()
2. How to make this operation when reading data in asynchronously,
using BeginReceive()



处理设计从同步到<的任何转换都是一样的br />
异步。保存在局部变量中的状态只需将

移入实例变量中,可以在每个网络操作完成后访问这些变量。


Pete

The same way you''d deal with any transition in design from synchronous to
asynchronous. State that is kept in local variables simply has to be
moved into instance variables that can be accessed as each network
operation completes.

Pete


10月24日,1:36 * pm,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:
On Oct 24, 1:36*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

周五,2008年10月24日10:08:42 -0700 ,puzzlecracker< ironsel2 ... @ gmail.com> *

写道:
On Fri, 24 Oct 2008 10:08:42 -0700, puzzlecracker <ironsel2...@gmail.com>*
wrote:

说我想安排内部缓冲区中的字节以某种方式。我

在套接字中接收这些字节。
Say I want to arrange bytes in the internal buffer in a certain way. I
receive those bytes in the socket.


一种解决方案是读入套接字:
One solution is to read in socket in pieces:


* * * * * * byte [] buffer = new byte [65536];

* * * * * * int index = 0;

* * * * * * m_socket.Receive(buffer,8,SocketFlags.None);

* * * * * * * index + = 8;

* * * * * * //读取消息的其余部分

* * * * * * // int msgLength是缓冲区中的剩余字节数

* * * * * * m_socket.Receive(缓冲区, index,msgLen - index,

SocketFlags.None);
* * * * * * byte[] buffer = new byte[65536];
* * * * * * int index = 0;
* * * * * * m_socket.Receive(buffer , 8, SocketFlags.None);
* * * * * * *index += 8;
* * * * * * //read the rest of the message
* * * * * * //int msgLength is remaining number of bytes inthe buffer
* * * * * * m_socket.Receive(buffer , index, msgLen - index,
SocketFlags.None);


问题:
Questions:


1.如何计算* msgLength?
1. How to calculate *msgLength ?



如果通过缓冲区中的剩余字节数,则表示准备从套接字读取*

*,答案是你不要。 *出于同样的原因,使用像Poll()或Select()这样的方法*

无法提供任何关于*

的保证当你真正尝试从中读取时会发生什么套接字,没有*

以*

的方式计算套接字缓冲区中尚未读取的字节的可靠方法,这将允许您依赖于计算在稍后的电话中*

接收()。


你能做的最好的事情就是为Receive()提供一个只有很大的缓冲区* />
足以满足您希望获得的给定消息的剩余字节数(我是*

,假设前八个字节中的数据以某种方式允许您*

计算得出)。 *然后继续阅读循环,直到你读到*

多个字节,减少你提供给每个*

迭代的Receive()的长度考虑到已经读取的字节数。


这就是说,如果你不需要*

效率,这只是你应该做的事情我/ O。 *你的代码和网络缓冲区之间来回交换很多开销,如果有很多数据来源*

通过,你最终可能会迫使网络驱动程序抛出*

的传入数据,并让另一端再次发送它。


一个更好的方法是为了网络I / O代码总是只读取*

尽可能多的数据,并让你的代码的另一层处理解析*

有用的消息。 *这样做实际上不一定是*

直接解决上述问题(毕竟,网络i / o层是*

可能仍然会提供数据作为一个流),但它确实将*

转移到你有更多控制权的域中。


If by "remaining number of bytes in the buffer", you mean ready to be read *
*from the socket, the answer is "you don''t". *For the same reason that *
using a method like Poll() or Select() cannot provide any guarantees about *
what will happen when you actually try to read from the socket, there''s no *
reliable way to calculate the bytes yet unread in the socket buffer in a *
way that will allow you to depend on the calculation in a later call to *
Receive().

The best you can do is provide a buffer to Receive() that is just large *
enough for the remaining bytes you expect to get for a given message (I''m*
assuming that the data in the first eight bytes in some way allow you to *
calculate that). *Then just keep reading in a loop until you''ve read that *
many bytes, decreasing the length you offer to Receive() with each *
iteration to take into account the number of bytes read already.

That said, this is only something you should be doing if you don''t need *
efficient i/o. *There''s a lot of overhead going back and forth between *
your code and the network buffers, and if there''s a lot of data coming *
through, you can wind up forcing the network driver to have to throw out *
incoming data, and making the other end send it again.

A much better approach is for the network i/o code to always just read as*
much data as it can, and let another layer of your code deal with parsing*
that out into usable messages. *Doing so won''t actually necessarily *
address the above question directly (after all, the network i/o layer is *
probably still going to be delivering data as a stream), but it does shift *
it into a domain where you have a bit more control.


2.如何在异步读取数据时进行此操作,

使用BeginReceive()
2. How to make this operation when reading data in asynchronously,
using BeginReceive()



与处理任何设计转换的方式相同从同步到*

异步。 *保存在局部变量中的状态只需要*

移入实例变量,可以在每个网络*

操作完成时访问。


Pete


The same way you''d deal with any transition in design from synchronous to*
asynchronous. *State that is kept in local variables simply has to be *
moved into instance variables that can be accessed as each network *
operation completes.

Pete



是否有另一个.net小组专门处理与

csharp和.net general相关的网络问题?

Is there another .net group dedicated to networking issues related to
csharp and .net general?


2008年10月24日星期五13:07:02 -0700,puzzlecracker< ir ********* @ gmail.com>

写道:
On Fri, 24 Oct 2008 13:07:02 -0700, puzzlecracker <ir*********@gmail.com>
wrote:

还有另一个.net小组致力于与

csharp和.net general相关的网络问题吗?
Is there another .net group dedicated to networking issues related to
csharp and .net general?



没有与C#相关的网络问题。 API是与语言无关的



至于.NET中特定于网络代码的新闻组,没有我是

意识到了。但是,请注意 - 正如我之前提到的那样 - 这些问题甚至不是.NET特有的。它们要么是网络中固有的,要么是b $ b,或者是在Winsock中,.NET网络API是建立起来的。


如果你想要一个比网络问题更具针对性的新闻组而不是这个,你可以试试alt.winsock.programming或

comp.os.ms-windows.programmer .tools.winsock。还有其他新闻组

比那些更普遍,甚至不是特定于Winsock。


Pete

There are no networking issues related to C#. The API is
language-agnostic.

As far as a newsgroup specific to network code in .NET, none that I''m
aware of. However, note that -- as I''ve mentioned before -- to a large
extent these issues aren''t even specific to .NET; they are either inherent
in networking generally, or in Winsock, on which the .NET networking API
is built.

If you want a newsgroup that is more specific to networking questions than
this one, you might try alt.winsock.programming or
comp.os.ms-windows.programmer.tools.winsock. There are other newsgroups
that are even more general than those, not even being specific to Winsock.

Pete


这篇关于读入缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆