0

So, I am trying to send an image between the server and client, however, somehow when the server receives the image, the image does not render all of it.

Client Side code:

//button click
Bitmap tImage = new Bitmap(@"C:\Users\Milan\Downloads\guitarstill.gif");

            byte[] bStream;
            //if (string.IsNullOrEmpty(tbPayload.Text)) return;
            try
            {
                bStream = imageToByteArray(tImage);
                if (mTcpClient != null)
                {
                    if (mTcpClient.Client.Connected)
                    {
                        mTcpClient.GetStream().BeginWrite(imageToByteArray(tImage), 0, imageToByteArray(tImage).Length, onCompleteWriteToServer, mTcpClient);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }


//Within another method:
public byte[] imageToByteArray(System.Drawing.Image imageIn) {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //richTextBox1.Text = ms.ToArray().ToString();
            return ms.ToArray();
}

Server Side Code:

void onCompleteReadFromTCPClientStream(IAsyncResult iar)
 {
TcpClient tcpc;
        int nCountReadBytes = 0;
        string strRecv;
        ClientNode cn = null;

        try
        {
            lock (mlClientSocks)
            {
                tcpc = (TcpClient)iar.AsyncState;

                cn = mlClientSocks.Find(x => x.strId == tcpc.Client.RemoteEndPoint.ToString());

                nCountReadBytes = tcpc.GetStream().EndRead(iar);

                if (nCountReadBytes == 0)// this happens when the client is disconnected
                {
                    MessageBox.Show("Client disconnected.");
                    mlClientSocks.Remove(cn);
                    lbClients.Items.Remove(cn.ToString());
                    return;
                }

                strRecv = Encoding.ASCII.GetString(cn.Rx, 0, nCountReadBytes);
                //strRecv = Encoding.ASCII.GetString(mRx, 0, nCountReadBytes);

                if (strRecv.StartsWith("GIF"))
                {
                    //MemoryStream ms = new MemoryStream(cn.Rx);

                    //Image x = (Bitmap)((new ImageConverter()).ConvertFrom(cn.Rx));


                    pictureBox1.Image = byteArrayToImage(cn.Rx);

                    /*
                    lock (mlClientSocks)
                    {

                        //if (cn != null && cn.tclient != null && cn.tclient.Client.Connected)
                        //{
                        //foreach (var clients in spectatorsIPAndPort)
                        //{
                        cn = mlClientSocks.Find(x => x.strId == clients);
                        cn.Tx = new byte[512];

                        cn.Tx = Encoding.ASCII.GetBytes(strRecv);
                        cn.tclient.GetStream().BeginWrite(cn.Tx, 0, cn.Tx.Length, onCompleteWriteToClientStream, cn.tclient);
                        //Console.WriteLine("Sent Number of Clients via Request: " + Encoding.UTF8.GetString(cn.Tx) + " To " + clients);
                        //}
                        //}

                    }
                    */



                    //printLine(DateTime.Now + " - " + cn.ToString() + ": " + strRecv);


                }

                cn.Rx = new byte[512];

                tcpc.GetStream().BeginRead(cn.Rx, 0, cn.Rx.Length, onCompleteReadFromTCPClientStream, tcpc);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            lock (mlClientSocks)
            {
                printLine("Client disconnected: " + cn.ToString());
                mlClientSocks.Remove(cn);
                lbClients.Items.Remove(cn.ToString());
            }

        }
    }


//within another method
public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn, 0, byteArrayIn.Length);
            //ms.Position = 0;
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

My Output:

enter image description here

Expected output: - this full image

enter image description here

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
Milan
  • 763
  • 3
  • 10
  • 18
  • Sorry, let me copy and paste the full code from both server and client – Milan Mar 02 '17 at 14:21
  • 1
    No, don't do that. Create a **minimal** code example that reproduces the problem. All wrapper code you wrote is irrelevant to the problem. – CodeCaster Mar 02 '17 at 14:21
  • well what i mean is, the most relevant. – Milan Mar 02 '17 at 14:21
  • But pay attention to the other parts asked for - *Complete* and *Verifiable*. There's a reason why there's a specific page for MCVEs. – Damien_The_Unbeliever Mar 02 '17 at 14:22
  • 3
    And, almost certainly, it's because as CodeCaster says, you're probably *ignoring* a value that tells you how many bytes were *actually* received and you're *expecting* messaging - TCP is a stream of bytes - there's no guarantee that a call to `Send` at one end will be matched 1-1 by calls to `Receive` at the other. If you want messaging, it's up to you to implement that or to move to a higher-level protocol that gives you messaging. – Damien_The_Unbeliever Mar 02 '17 at 14:25
  • A description of how message framing works: http://stackoverflow.com/a/37352525/3740093 – Visual Vincent Mar 05 '17 at 18:36

0 Answers0