简体   繁体   English

从网络流读取C#

[英]C# Read from a Networkstream

Im trying to send an object( in my case an image) over a networkstream.. however- im not getting the full image.. 我试图通过网络流发送一个对象(在我的情况下为图像)。但是,我没有获得完整的图像。

This is the client code: 这是客户端代码:

  private void Form1_Load(object sender, EventArgs e)
    {


       TcpClient c = new TcpClient();
        c.Connect("10.0.0.4", 10);
       NetworkStream ns = c.GetStream();

       Bitmap f = GetDesktopImage();
       byte[] buffer = imageToByteArray(f);
       byte[] len = BitConverter.GetBytes(buffer.Length);        
       MemoryStream stream = new MemoryStream();
           stream.Write(len, 0, len.Length);
            stream.Write(buffer, 0, buffer.Length);
            stream.Position = 0;
            stream.CopyTo(ns);

    }

As you can see i write the entire content to a regular MemoryStream first(because i dont want to use twice the NetworkStream - only then, i copy the MemoryStream content into the NetworkStream . 如您所见,我首先将整个内容写入常规的MemoryStream中 (因为我不想使用两次NetworkStream -仅在那时,我将MemoryStream内容复制到NetworkStream

The server code: 服务器代码:

private void Form1_Load(object sender, EventArgs e)
    {
        TcpListener tl = new TcpListener(IPAddress.Any, 10);
        tl.Start();
        TcpClient c = tl.AcceptTcpClient();
          network = new NetworkStream(c.Client);

          byte[] buff = new byte[4];
          network.Read(buff, 0, 4);
          int len = BitConverter.ToInt32(buff, 0);

          buff = new byte[len];
          network.Read(buff, 0, buff.Length);
          pictureBox1.Image = byteArrayToImage(buff);
          Thread th = new Thread(method);


    }

Now when i run both application im getting only the top part of the captured image... It's even more odd because writing directly both to a network stream works perfect... For example: 现在,当我运行这两个应用程序时,我只获取捕获图像的顶部...这更奇怪,因为直接将两者都写入网络流非常完美...例如:

 ns.Write(len, 0, len.Length);
   ns.Write(buffer, 0, buffer.Length);

This would work fine and i'll get the full image in the other side.. but i dont want to use it twice(it's just an example, in my real project i would have to use it alot so i would like to reduce the network usage as much as posibble ,and not trigger it for every single data). 这将工作正常,我将在另一侧获得完整图像。.但是我不想使用它两次(这只是一个例子,在我的真实项目中,我不得不大量使用它,因此我想减少网络使用率最高,而不是针对每个数据触发它)。

Why it's not working using simply the CopyTo method? 为什么仅使用CopyTo方法不能正常工作?

I would appreciate any help! 我将不胜感激任何帮助!

Thanks 谢谢

In your server code you are ignoring the result of NetworkStream.Read which is the actual number of bytes read. 在服务器代码中,您将忽略NetworkStream.Read的结果,该结果是实际读取的字节数。 This can be less than the number of bytes you actually requested. 这可能少于您实际请求的字节数。 You need to keep calling Read until you have received all the bytes you need. 您需要保持调用Read的状态,直到收到所需的所有字节为止。

The differences you seeing between 1 or multiple writes has to do with the buffering/timing in the network stack. 您看到的1次或多次写入之间的差异与网络堆栈中的缓冲/定时有关。 I suspect you just getting lucky receiving the image when doing a single write - this will not always be the case! 我怀疑您在一次写入时会很幸运地收到图像-情况并非总是如此! You need to handle the result of Read as explained above. 需要按照上述说明处理Read的结果。

Code Example: 代码示例:

void ReadBuffer(byte[] buffer, int offset, int count)
{
    int num;
    int num2 = 0;
    do
    {
        num2 += num = _stream.Read(buffer, offset + num2, count - num2);
    }
    while ((num > 0) && (num2 < count));
    if (num2 != count)
        throw new EndOfStreamException
            (String.Format("End of stream reached with {0} bytes left to read", count - num2));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM