0

i did create socket application to send and receive images over internet

i did test the application locally and on vmware , it did works fine

also test it on some devices over internet also worked fine

but there is other devices that the Server application receive only small amount of the sent data

here is an image of the server image

and this is the server code

while(true)
        {
            Socket socket = listener.AcceptSocket();
            label4.Text = "Connected";
            byte[] image = new byte[99999999];
            int offset = -1;
            int ite = 0;
            while (true)
            {
                
                byte[] data = new byte[1024];
                int count = socket.Receive(data);
                if (count > 0)
                {
                    String txt = Encoding.ASCII.GetString(data, 0, count);

                    if (txt.Contains("hello"))
                    {
                        label8.Text = txt;
                        continue;
                    }
                    if (!txt.Contains("end"))
                    {
                        ite += 1;
                        label7.Text = ite.ToString();
                        label12.Text = offset.ToString();
                        for (int i = 0; i <= count - 1; i++)
                        {
                            offset += 1;
                            image[offset] = data[i];                                
                        }
                        try
                        {
                            MemoryStream stream = new MemoryStream(image, 0, offset);
                            Image x = Image.FromStream(stream);
                            pictureBox1.Image = x;
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    else
                    {
                        label7.Text = ite.ToString();
                        ite = 0;
                        label5.Text = label2.Text;
                        label2.Text = offset.ToString();
                        MemoryStream stream = new MemoryStream(image, 0, offset);
                        Image x = Image.FromStream(stream);
                        pictureBox1.Image = x;

                        image = new byte[99999999];
                        offset = -1;
                        break;

                    }


                }
            }
        }

and this is the client code

 while (true)
            {
                TcpClient client = new TcpClient(ip, port);
                NetworkStream stream = client.GetStream();
                byte[] data = Capture();
                int dataSize = 1024;
                int offset = 0;
                byte[] start = ASCIIEncoding.ASCII.GetBytes("hello " + data.Length.ToString());                    
                stream.Write(start, 0, start.Length);                
                while (offset < data.Length)
                {
                    int sum = offset + dataSize;
                    if (sum > data.Length)
                    {
                        dataSize = data.Length - offset;
                    }
                    stream.Write(data, offset, dataSize);

                    offset += dataSize;
                }
                Thread.Sleep(1000);
                byte[] end = ASCIIEncoding.ASCII.GetBytes("end");
                stream.Write(end, 0, end.Length);

                client.Close();
                Thread.Sleep(interval);
                Console.WriteLine("iter=" + iteration.ToString());
            }
11
  • 2
    1) The server never reads from socket into image. 2) Your code incorrectly assumes that a socket read will return a complete message. TCP/IP is a streaming protocol, and if you expect 100 bytes, you must read until you've accumulated 100 bytes. Commented Aug 8, 2021 at 14:41
  • i don't get what are you trying to explain Commented Aug 8, 2021 at 15:15
  • but let me explain the code here first the server read the data , check if the received amount of bytes more than 0 byte then fill the first 1024 into an image array then display this 1024 byte of image into the picture box Commented Aug 8, 2021 at 15:17
  • 1
    Instead of inventing your own network protocol on top of TCP/IP, you should probably use an existing one, like HTTP. EG learn.microsoft.com/en-us/dotnet/api/… Commented Aug 8, 2021 at 15:22
  • 1
    That is a classic symptom of not understanding how to use TCP/IP sockets. Every read must be in a loop, as neither Socket.Receive nor NetworkStream.Read wait until the requested number of bytes have been received. And often you only see partial reads in some network scenarios. Commented Aug 8, 2021 at 15:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.