none
Socket: why stops of processe images? RRS feed

  • Question

  • I have a android code that was made to send screenshot continuously to server.

    On server i have a socket special only to receive these screenshots, but my trouble is that only the first screenshot is processed.

    Why this is happening?

    Here is the server:

    Bitmap _buffer;

    private class StateObject { public Socket workSocket = null; public const int BufferSize = 1024 * 10; public byte[] buffer = new byte[BufferSize]; } private void StartListening() { IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 100); Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { listener.Bind(localEndPoint); listener.Listen(100); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); } catch (Exception e) { MessageBox.Show(e.ToString()); } } private void AcceptCallback(IAsyncResult ar) { Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(Read_Image), state); } private void Read_Image(IAsyncResult ar) { StateObject so = (StateObject)ar.AsyncState; Socket s = so.workSocket; try { int read = s.EndReceive(ar); if (read > 0) { RecvScreenshot(s); }

    Array.Clear(so.buffer, 0, so.buffer.Length);
                    s.BeginReceive(so.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(Read_Image), so);

    } catch (Exception e) { MessageBox.Show(e.ToString()); } } private void RecvScreenshot(Socket s) { var lengthData = new byte[4]; var lengthBytesRead = 0; do { var read = s.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None); if (read == 0) return; lengthBytesRead += read; } while (lengthBytesRead < lengthData.Length); Array.Reverse(lengthData); var length = BitConverter.ToInt32(lengthData, 0); if (length > 0) { var imageData = new byte[length]; var imageBytesRead = 0; do { var read = s.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None); if (read == 0) return; imageBytesRead += read; } while (imageBytesRead < imageData.Length); using (var stream = new MemoryStream(imageData)) { var bitmap = new Bitmap(stream); Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap }); } } } private delegate void ImageCompleteDelegate(Bitmap bitmap); private void ImageComplete(Bitmap bitmap) { if (_buffer != null) { _buffer.Dispose(); } _buffer = new Bitmap(bitmap); pictureBox1.Size = _buffer.Size; pictureBox1.Invalidate(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (_buffer == null) return; e.Graphics.DrawImage(_buffer, 0, 0); }


    Here is client (Java Android), normally on android not is necessary send a prefixed string before, but to receive on C# this was necessary (or nothing could be received):

    DataOutputStream dos;

    try { dos = new DataOutputStream(SocketBackgroundService.clientSocket.getOutputStream()); dos.write("SCREEN".getBytes(StandardCharsets.UTF_8)); dos.writeInt(array.length); dos.write(array, 0, array.length); dos.flush(); } catch (IOException e) { e.printStackTrace(); }





    • Edited by FLASHCODER Monday, November 5, 2018 11:34 AM
    Sunday, November 4, 2018 2:41 PM

Answers

  • Your code is not listening for the SCREEN prefix.  The characters SCRE if read as an integer comes out to about 1.7GB.

    You're swapping the bytes in the length.  Are you sure DataOutputStream writes integers in big-endian format?


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by FLASHCODER Tuesday, November 6, 2018 4:24 AM
    Tuesday, November 6, 2018 12:32 AM

All replies

  • EDITION:

    The first image is received with a length of 67KB for example (here is ok), after is received a strange length like 1,7GB. Why? i don't know.

    private readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
    private string SizeSuffix(Int64 value, int decimalPlaces = 1)
    {
        if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
        if (value < 0) { return "-" + SizeSuffix(-value); }
        if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
    
        int mag = (int)Math.Log(value, 1024);
    
        decimal adjustedSize = (decimal)value / (1L << (mag * 10));
    
        if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
        {
            mag += 1;
            adjustedSize /= 1024;
        }
    
        return string.Format("{0:n" + decimalPlaces + "} {1}",
            adjustedSize,
            SizeSuffixes[mag]);
    }
    
    ....
    
    // RecvScreenshot() method
    
    if (length > 0)
    {
        var imageData = new byte[length];
        var imageBytesRead = 0;
    
        MessageBox.Show(SizeSuffix(length));
    
    ....


    • Edited by FLASHCODER Monday, November 5, 2018 11:32 AM
    Monday, November 5, 2018 12:13 AM
  • Hi FLASHCODER,

    Thank you for posting here.

    Since your question is more related Android, you could post a new thread in Android forum for suitable support.

    https://androidforums.com/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 5, 2018 6:51 AM
    Moderator
  • Hi FLASHCODER,

    Thank you for posting here.

    Since your question is more related Android, you could post a new thread in Android forum for suitable support.

    https://androidforums.com/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    No, because the bug is on C# code. Thank you.

    Monday, November 5, 2018 11:35 AM
  • Your code is not listening for the SCREEN prefix.  The characters SCRE if read as an integer comes out to about 1.7GB.

    You're swapping the bytes in the length.  Are you sure DataOutputStream writes integers in big-endian format?


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by FLASHCODER Tuesday, November 6, 2018 4:24 AM
    Tuesday, November 6, 2018 12:32 AM