0

I have a C# app.

It uploads images to my server and downloads them when necessary.

This is my client:

sb.Clear();
sb.Append(GeneralTags.ACTION_ADD);
sb.Append(Shared.DLE);
sb.Append("GALLERY");
sb.Append(Shared.DLE);
sb.Append(Shared.CurrentClientId);
sb.Append(Shared.DLE);
sb.Append(JsonConvert.SerializeObject(gallery));
sb.Append(Shared.DLE);
byte[] byteArray = System.Web.HttpUtility.UrlEncodeToBytes(sb.ToString());
int packetCount = 0;

using (TcpClient clientSocket = new TcpClient())
{
    clientSocket.Connect(GeneralTags.RASPBERRY_PI_IP_ADDRESS, GeneralTags.RASPBERRY_PI_PORT);
    using (NetworkStream serverStream = clientSocket.GetStream())
    {
        serverStream.Write(byteArray, 0, byteArray.Length);
        serverStream.Flush();

        List<byte> requestInBytes = new List<byte>();

        if (serverStream.CanRead)
        {
            int i = 0;
            Byte[] bytesIn = new Byte[1024];
            do
            {
                i = serverStream.Read(bytesIn, 0, bytesIn.Length);
                byte[] receivedBuffer = new byte[i];
                Array.Copy(bytesIn, receivedBuffer, i);

                if (packetCount == 0)
                {
                    packetCount = BitConverter.ToInt32(receivedBuffer, 0);
                }
                requestInBytes.AddRange(receivedBuffer);
            } while (packetCount < requestInBytes.Count);

            serverStream.Close();


            var json = Encoding.UTF8.GetString(requestInBytes.ToArray(), 0, requestInBytes.ToArray().Length);
            var galleryback = JsonConvert.DeserializeObject<Gallery>(json);

            using (MemoryStream ms = new MemoryStream())
            {
                System.Diagnostics.Debug.WriteLine("BACK: " + galleryback.ImageData.Length.ToString());
                ms.Write(galleryback.ImageData, 0, galleryback.ImageData.Length);
                Shared.ViewImage(Image.FromStream(ms, true));
            }
        }
        else
        {
            MessageBox.Show("Local Device Error");

        }
    }
}

This is my class object:

public class Gallery
{
    public string Title { get; set; }
    public string Description { get; set; }
    public byte[] ImageData { get; set; }
    public bool IsUploaded { get; set; }
    public Int64 GalleryId { get; set; }
    public string JobRef { get; set; }
}

If we assume that my server is sending the entire data array back correctly (I have checked the bytes sent from server with the bytes received by my client)...

I get these type of errors:

Unterminated string. Expected delimiter: ". Path 'ImageData', line 1, position 5515.

Unexpected character encountered while parsing value: �. Path '', line 0, position 0.

How can I solve this?

thanks

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

1 Answers1

0

It looks like you are sending a byte array with the serialized JSON as only a part of it (you append other things to the string, like GeneralTags.ACTION_ADD and so on), but when you receive data you try to deserialize the whole thing. Of course, since you do not have data in JSON format only, this results in DeserializeObject not being able to parse the data passed to it. You need to extract the serialized object in some way, for example using regex or some calculation involving substrings.

Kapol
  • 6,383
  • 3
  • 21
  • 46
  • Hi thanks for your observation. I did start off with just using a Jason object and using an image object. But the image never got through ok. I was told Json does not work well with images. So I thought upload it as a byte array – Andrew Simpson Nov 21 '15 at 20:51
  • Hi I am away from my PC but will look at this when back. Thanks – Andrew Simpson Nov 21 '15 at 20:52
  • @AndrewSimpson I don't think the problem lies in using a byte array. What I'm saying is that the string encoded from the array has more information than `DeserializeObject` should be called with. – Kapol Nov 21 '15 at 20:54
  • Hi but I know my server is extracting the Json object and is sending just that back. I do know that I have to use an encoder on binary data - I will have to use a base64 encoder. The problem is decoding and encoding the complete packet from client to server and server to client. – Andrew Simpson Nov 21 '15 at 21:00
  • And I have to do it before serlizing it as a Json object. – Andrew Simpson Nov 21 '15 at 21:01