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());
            }
  • 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. – David Browne - Microsoft Aug 08 '21 at 14:41
  • i don't get what are you trying to explain – john smith Aug 08 '21 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 – john smith Aug 08 '21 at 15:17
  • the client is continuously send 1024 byte from the image until it reach the end of the image – john smith Aug 08 '21 at 15:20
  • 1
    Instead of inventing your own network protocol on top of TCP/IP, you should probably use an existing one, like HTTP. EG https://learn.microsoft.com/en-us/dotnet/api/system.net.httplistener?view=net-5.0 – David Browne - Microsoft Aug 08 '21 at 15:22
  • okay i will try it, but what is the issue with my code as i mention it works on some devices over the internet and some devices there is issues like the one i mention above – john smith Aug 08 '21 at 15:28
  • 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. – David Browne - Microsoft Aug 08 '21 at 15:33
  • You need *message framing*. See e.g. [Broken TCP messages](https://stackoverflow.com/q/7257139/3744182), [Async socket client using TcpClient class C#](https://stackoverflow.com/q/41718342/3744182). – dbc Aug 08 '21 at 16:02
  • okay thank you all for the solution i will test them all – john smith Aug 08 '21 at 17:38
  • I second the suggestion to use a higher level protocol. You might also want to check out the various [message queues](http://mikehadlow.blogspot.com/2011/04/message-queue-shootout.html). They will likely be much easier to use correctly than raw sockets. – JonasH Aug 08 '21 at 20:32
  • i did create message framing and here is my code for server and client pastebin.com/cS1Avh5y but the issue still exists only receive part of the packets not all of it , about the httplistener i don't think i will be able to interact with it using socket – john smith Aug 09 '21 at 10:17

0 Answers0