English 中文(简体)
C# 接收从袖珍中获取数据的业绩方法?
原标题:C# performance methods of receiving data from a socket?

Let s assume we have a simple internet socket, and it s going to send 10 megabytes (because I want to ignore memory issues) of random data through. Is there any performance difference or a best practice method that one should use for receiving data? The final output data should be represented by a byte[]. Yes I know writing an arbitrary amount of data to memory is bad, and if I was downloading a large file I wouldn t be doing it like this. But for argument s sake let s ignore that and assume it s a smallish amount of data. I also realise that the bottleneck here is probably not the memory management but rather the socket receiving. I just want to know what would be the most efficient method of receiving data.

可以考虑的几种方法是:

  1. 在缓冲区满后,在名单和最后名单中增加名单和缓冲。 ToArray() to receive the byte[]

  2. 在其完整构造下一代后,向记忆流书写缓冲。 缩略语并读到所有文本,以便获得间接产出。

这样做是否有效率更高/更好的办法?

最佳回答

仅写到<条码>MemoryStream,然后打电话>>>:ToArray——为你建造一个适当规模的旁边阵列。 这样,<代码>List<byte>就好像,但使用<代码>MemoryStream将是一个更为简单的例子。

问题回答

诚然,若约·斯凯特的回答是巨大的(与往常一样),但没有任何法律,因此我的解释就是这样。 (为我工作罚款)

using (var mem = new MemoryStream())
{
    using (var tcp = new TcpClient())
    {
        tcp.Connect(new IPEndPoint(IPAddress.Parse("192.0.0.192"), 8880));
        tcp.GetStream().CopyTo(mem);
    }
    var bytes = mem.ToArray();
}

(没有将两个<代码>使用?) 如果你想去掉,你也许会想在抽出时间来审视收到的tes子之前释放 connection。

该代码将接收多种包装材料,并汇总其数据。 因此,简单地接收在连接期间发送的所有笔录数据,是非常有效的。

贵国数据如何编码? 它是简单的ASCII,还是其他东西,如UTF-8/Unicode?

如果是ASCII,那么你就只能把所需面积(从答复内容的主人中标明面积)分配给斯特林(主人),并在使用编码将其转换成护法后,继续把你的数据提供给建筑商。 ASCII.

如果是统法协会/UTF8,那么你就有一个问题,即你不能仅仅把 En(买,0, by)放在tes读物上,因为 by是 在这种编码中,阅读可能不会构成一种逻辑的碎块。 就本案而言,你需要把整个实体的记忆(或档案),然后读到该档案,并用编码加以编码。

你可以写到记忆流,然后使用透镜或像获得数据这样的东西。 你在数据方面做了哪些工作? 我询问,因为从记忆的角度来看,在接收数据而不是存储全部内容时,将所输入的数据写到档案或数据库表会更为有效。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签