c# SslStream.Read循环问题

发布于 2024-11-13 13:09:40 字数 1685 浏览 3 评论 0原文

我一直在通过创建一个应用程序来学习 C#,但我遇到了一个我一直在努力解决的障碍。

基本上我有下面的代码,这是我用来从我设置的网络流中读取的代码。它可以工作,但每次 sslstream.Read() 解锁时它只读取 1 个数据包。这导致消息大量积压。

我正在尝试做的是,如果流读取的部分包含多个数据包,则将它们全部读取。

我曾多次尝试解决这个问题,但最终却陷入了一堆混乱的代码中。

如果有人可以帮助我,我将不胜感激!

(每个数据包的前 4 个字节是数据包的大小。数据包范围在 8 字节到 28,000 字节之间)

        SslStream _sslStream = (SslStream)_sslconnection;
        int bytes = -1;
        int nextread = 0;
        int byteslefttoread = -1;
        byte[] tmpMessage;
        byte[] buffer = new byte[3000000];
        do
        {
            bytes = _sslStream.Read(buffer, nextread, 8192);
           int packetSize = BitConverter.ToInt32(buffer, 0);
            nextread += bytes;
            byteslefttoread = packetSize - nextread;

            if (byteslefttoread <= 0)
            {
                int leftover = Math.Abs(byteslefttoread);
                do
                {
                    tmpMessage = new byte[packetSize];
                    Buffer.BlockCopy(buffer, 0, tmpMessage, 0, packetSize);

                    PacketHolder tmpPacketHolder = new PacketHolder(tmpMessage, "in");
                    lock (StaticMessageBuffers.MsglockerIn)
                    {
                        //puts message into the message queue.. not very oop... :S
                        MessageInQueue.Enqueue(tmpPacketHolder);
                    }
                }
                while (leftover > 0);

                Buffer.BlockCopy(buffer, packetSize , buffer, 0, leftover);
                byteslefttoread = 0;
                nextread = leftover;

            }

        } while (bytes != 0);

I've been learning C# by creating an app and i've hit a snag i'm really struggling with.

Basicly i have the code below which is what im using to read from a network stream I have setup. It works but as its only reading 1 packet for each time the sslstream.Read() unblocks. It's causes a big backlog of messages.

What im looking at trying to do is if the part of the stream read contains multiple packets read them all.

I've tried multiple times to work it out but i just ended up in a big mess of code.

If anyone could help out I'd appreciate it!

(the first 4bytes of each packet is the size of the packet.. packets range between 8 bytes and 28,000 bytes)

        SslStream _sslStream = (SslStream)_sslconnection;
        int bytes = -1;
        int nextread = 0;
        int byteslefttoread = -1;
        byte[] tmpMessage;
        byte[] buffer = new byte[3000000];
        do
        {
            bytes = _sslStream.Read(buffer, nextread, 8192);
           int packetSize = BitConverter.ToInt32(buffer, 0);
            nextread += bytes;
            byteslefttoread = packetSize - nextread;

            if (byteslefttoread <= 0)
            {
                int leftover = Math.Abs(byteslefttoread);
                do
                {
                    tmpMessage = new byte[packetSize];
                    Buffer.BlockCopy(buffer, 0, tmpMessage, 0, packetSize);

                    PacketHolder tmpPacketHolder = new PacketHolder(tmpMessage, "in");
                    lock (StaticMessageBuffers.MsglockerIn)
                    {
                        //puts message into the message queue.. not very oop... :S
                        MessageInQueue.Enqueue(tmpPacketHolder);
                    }
                }
                while (leftover > 0);

                Buffer.BlockCopy(buffer, packetSize , buffer, 0, leftover);
                byteslefttoread = 0;
                nextread = leftover;

            }

        } while (bytes != 0);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-11-20 13:09:40

如果您使用 .Net 3.5 或更高版本,我强烈建议您查看 Windows Communication Foundation (wcf )。它会简单地处理您尝试通过网络执行的任何操作。

另一方面,如果您这样做纯粹是为了教育目的。
请查看此链接。最好的选择是以较小的增量从流中读取数据,并将该数据输入到另一个流中。一旦您确定了消息所需的数据长度,您就可以将第二个流切割成消息。您可以设置一个外部循环,在其中检查可用字节并等待其值 > 0 开始下一条消息。还应该注意的是,任何网络代码都应该在自己的线程上运行,以免阻塞 UI 线程。

If you are using .Net 3.5 or later I would highly suggest you look into Windows Communication Foundation (wcf). It will simply anything you are trying to do over a network.

On the other hand, if you are doing this purely for educational purposes.
Take a look at this link. Your best bet is to read from the stream in somewhat smaller increments, and feed that data into another stream. Once you can identify the length of data you need for a message, you can cut the second stream off into a message. You can setup an outside loop where available bytes are being checked and wait until its value is > 0 to start the next message. Also should note, that any network code should be running on its own thread, so as to not block the UI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文