c# 流数据读取不正确 [英] c# Stream data not reading correctly

查看:42
本文介绍了c# 流数据读取不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决问题是我假设它在我告诉它读取时立即获取了所有数据,因此它从我的 READ SCREENSHOT 方法中逃逸并将数据包转储到我的数据包解析器中,该解析器继续向我的控制台发送垃圾邮件 >.<

SOLVED Issue was I was assuming it got all the data at once when i told it to read, so it was escaping out of my READ SCREENSHOT method and dumping the data pack into my packet parser, which proceeded to spam my console with junk >.<

谢谢乔恩.

我有另一个新问题,这次我无法弄清楚我的数据流有什么问题,我目前正在尝试将捕获的图像文件流式传输到中央网络服务器,我设置了客户端 > 服务器,正在通话很好,整天来回传递数据,但是当我尝试调用我的屏幕截图功能并发送它时,这就是我遇到问题的地方.

I have another new problem, this time I cant figure out whats wrong with my data streams, I'm currently attempting to stream image files that are captured to a central networked server, I have the Client > Server set up, talking just fine, passing data back and forth all day long, but when i attempt to call my screenshot function and send it, thats where i get my issues.

我为此使用的当前代码是:

The current code im using for this is:

注意事项DataHandler 只是一个发送和接收数据的包装器,AddVar 是一个重载方法,它接受任何变量并通过流发送它 (new DataHandler(stream))

Notes DataHandler is just a wrapper for Sending and receiving data, AddVar is an overloaded method that takes any variable and sends it through a stream (new DataHandler(stream))

DataHandler 链接到 TCP 流

DataHandler is linked to the TCP Stream

ReadInt 和 ReadLong 等是相反的辅助函数,有助于使代码更易于以后维护.

ReadInt and ReadLong etc are the reverse, helper functions to facilitate making the code easier to maintain later.

此时服务器每秒向客户端发送一次 ping,如果客户端不忙于响应另一个数据包(传入的数据包在单个线程上运行),则客户端设置为响应该 ping

At this point the Server sends out a ping every second, to the client, the client is set to respond to that ping IF it isnt busy responding to another packet (the incoming packets run on a single thread)

服务器遵循相同的规则,每个客户端

Server follows the same rule, per client

在 SI.TakeScreenShotAndStream 将数据写入流,此方法获取客户端计算机的屏幕截图并将数据流式传输到任何传递的流,在本例中为内存流.

Writing the data to the stream at SI.TakeScreenShotAndStream, this method takes a screenshot of the client computer and streams the data to any stream passed, in this case the memorystream.

尾注

(客户端)

try
        {
            DataHandler.AddVariable((byte)50);
            memoryStream = new MemoryStream();
            SI.TakeScreenShotAndStream(memoryStream, quality);
            DataHandler.AddVariable(memoryStream.Length);
            memoryStream.Position = 0;
            memoryStream.CopyTo(clientStream);
            clientStream.Flush();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Disconnect();
        }

和服务器端

if (!Directory.Exists(LoadedSettings.Directory + name)) Directory.CreateDirectory(LoadedSettings.Directory + name);
            fileStream = File.Create(FullPath);
            long length = DataHandler.ReadLong();
            byte[] data = new byte[length];
            clientStream.Read(data, 0, (int)length);
            fileStream.Write(data, 0, (int)length);
            fileStream.Flush();
            fileStream.Close();

截屏并保存"功能有效,我可以将其流式传输到 FileStream 并将其直接保存到文件中,我的问题似乎是我不知道如何使用 MemoryStream 类,如果我真的关于如何做到这一点还很遥远,一个很好的内存流教程会很有帮助.

The "Take screenshot and save" function works, i can stream that to a FileStream and save it directly to a file, my problem seems to be that I have no clue how to work with the MemoryStream class if I'm really far off the mark on how to do this, a nice tutorial for memory streams would be helpful.

有一次我试图将 MemoryStream 转换为字节数组,但也没有奏效.

At one point I attempted to convery MemoryStream into a byte array but that didnt work either.

另外作为说明,这不仅仅是搞砸了,这两者都封装在 try/catch 语句和无效数据包(除了 1、2 和 50 atm 之外的任何内容)中,异常和无效的数据包编号都会被记录到控制台,当我运行此代码时,它会向控制台喷出太多声音,直到我将其关闭(我的程序中的任何地方都没有 console.beep 代码)

Also as a note, this doesnt just mess up, both of these are encapsulated in try/catch statements and invalid packets (anything except 1, 2 and 50 atm), both exceptions and packet numbers that are invalid get logged to the console, when i run this code it spews so much to the console that it beeps constantly until i shut it off (there is no console.beep code anywhere in my program)

任何帮助将不胜感激:)

Any assistance would be appreciated :)

推荐答案

这里是你阅读数据的方式:

It's the way you're reading the data here:

clientStream.Read(data, 0, (int)length);

相反,您应该使用 Read 返回的值:

Instead, you should use the value returned by Read:

// TODO: If you really want to handle more then 4GB per file, you'll need
// to change this...
int length = (int) DataHandler.ReadLong();
byte[] buffer = new byte[16 * 1024]; // Read up to 16K at a time

while (length > 0)
{
    int bytesRead = clientStream.Read(buffer, 0,
                                      Math.Min(length, buffer.Length));
    if (bytesRead <= 0)
    {
        // Throw an appropriate exception: the data is truncated
    }
    fileStream.Write(buffer, 0, bytesRead);
    length -= bytesRead;
}

这篇关于c# 流数据读取不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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