简体   繁体   中英

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 .

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?

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. 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.

The differences you seeing between 1 or multiple writes has to do with the buffering/timing in the network stack. 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.

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));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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