Yaron Ben Atar
Yaron Ben Atar

Reputation: 85

C# Sending image on tcp client/server

  im trying to make a client and server that the server sends image to the client

so far it possible to send only one image and the others arent accepted by mysterious error i tried to figure out the errors but no luck... this is the Server code: public void SendImage(Image img) {

        TcpClient tempClient = _Client;

        if (tempClient.Connected) //If the client is connected
        {
            NetworkStream stream = tempClient.GetStream();
            Bitmap bmp = new Bitmap(img);
            BinaryWriter bw = new BinaryWriter(stream);
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] buffer = ms.ToArray();
          byte[] lengthbts=  Get8LengthByte(buffer);
          stream.Write(lengthbts,0,8);
          stream.Write(buffer, 0, buffer.Length );
          stream.Flush();
        //   bw.Close();



        }

    }
    byte[] Get8LengthByte(byte[] bytes)
    {
        string length = bytes.Length.ToString();
        while (length.Length < 8)
        {
            length = "0" + length;
        }
        return Encoding.Default.GetBytes(length);
    }

and this is the client code

       NetworkStream stream = client.GetStream();
            //Bitmap bmp = new Bitmap(img);
            BinaryReader br = new BinaryReader(stream);
            byte[] datalen = new byte[8];
            br.Read(datalen, 0, 8);
            string lenStr = (Encoding.Default.GetString(datalen));
            int len = int.Parse(lenStr);//BitConverter.ToInt32(datalen, 0);//
            Console.WriteLine(len);
            byte[] buffer = new byte[len];
            br.Read(buffer, 0, buffer.Length);
              MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length);
            ms.Position = 0;
            Image img = Image.FromStream(ms);
            Invoke(new MethodInvoker(delegate()
            {
                pictureBox1.Image = img;
            }
            ));

the idea is to send the length of the pictures bytes first as 8 byte length for example if the byte length is 10 then the 8 byte length is "00000010" the first image recieved is very good and quick but the second return an error that can make the 8 byte length to integer the byte[] recieved are more like "?///???" and stuff like that if someone could help me to figure it out i will be very thankful

Upvotes: 0

Views: 4281

Answers (1)

ja_mesa
ja_mesa

Reputation: 1969

It's hard to tell. one thing I can suggest is: if you're getting the length value correct and the file is big > 4K then you should keep reading until the length is completed, for example:

int offset=0;
int bLeft=len;
byte[] buffer = new byte[len];
while (bLeft > 0)
{
    int ret = br.read(buffer, offset, bLeft);
    if (ret > 0)
    {
         bLeft-=ret;
         offset+=ret; 
    }
    else if (ret == 0) 
    {
        // the socket has been closed
    }
    else {
       // there is an error in the socket
    }
}

If your problems is sending the length of the file, i wouldn't mess with string and encoding, just send 4 bytes and read 4 bytes, like this

    byte[] bLength = new byte[4];
    int len = 123456;
    bLength[0] = (byte)((len & 0xff000000) >> 24);
    bLength[1] = (byte)((len & 0x00ff0000) >> 16);
    bLength[2] = (byte)((len & 0x0000ff00) >> 8);
    bLength[3] = (byte)(len & 0xff);
    // Send 4 bytes

    // Read only 4 bytes
    byte[] buff = new byte[4];
    br.read(buff, 0, 4);
    int length = (int)(buff[0] << 24) | (int)(buff[1] << 16) | (int)(buff[2] << 8) | buff[3];

Upvotes: 1

Related Questions