Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm sending screenshots every 500ms to the same computer but it only receives about 75% of the image, and the amount received varies with every screenshot. When sending to another computer on the same network (via ethernet), only a tiny fraction of the screen gets through.

Code to send screenshots:

sendDataToServer("$scr:" & Convert.ToBase64String(New ImageConverter().ConvertTo(screenshot, GetType(Byte()))))

Private Function screenshot() As Bitmap
        Static cap As Bitmap = Nothing
        If cap IsNot Nothing Then GC.RemoveMemoryPressure(cap.Width * cap.Height * 4)
        cap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppRgb)
        GC.AddMemoryPressure(cap.Width * cap.Height * 4)
        Using graph As Graphics = Graphics.FromImage(cap)
            graph.CopyFromScreen(0, 0, 0, 0, cap.Size, CopyPixelOperation.SourceCopy)
        End Using
        Return cap
    End Function


Receiving screenshots:

Private Sub readCallback(result As IAsyncResult)

        If closeMe Then Return

        Try
            networkStream = clientSocket.GetStream()
            Dim read As Integer = networkStream.EndRead(result)
            If read = 0 Then
                networkStream.Close()
                clientSocket.Close()
                Return
            End If

            Dim buffer As Byte() = TryCast(result.AsyncState, Byte())
            Dim data As String = Encoding.UTF8.GetString(buffer, 0, read)

            Dim cmd As String = Nothing
            If data.Contains(":") Then
                cmd = data.Split(":")(0)
                data = data.Remove(0, cmd.Length + 1)
            Else
                cmd = data
                data = Nothing
            End If

            Select Case (cmd) '! = request, $ = response
                Case "$scr"
                    main.crossThread(Utils.EnumCrossThreadFunction.updateWatchWindow, Utils.getArgs(Utils.bytesToImage(Convert.FromBase64String(data))))                
                Case "$ping"
                    main.getPingData()(username) = 1                              
            End Select

            awaitRequest()

        Catch ex As Exception

        End Try

    End Sub


Does anyone know the reason for this? The client also times out after a few seconds once screenshots start getting sent, telling me the connection gets saturated.

What I have tried:

I've tried using UDP and various different sets of TCP code.
Posted
Updated 3-Aug-16 8:43am

1 solution

You need to write proper socket code in VB first on both ends.

TCP can start and stop, and pick up where it left off, or to say it better, can transmit a chunk of data, then wait for the reply back saying data intact, and then cycle again. Like for Email or SMTP negotiations.

UDP is used mostly for streams, like streaming a video. It opens up a port, and starts streaming chunks of data to the receiver, so you have to control buffering data.

So write a good socket program first, test it, then just change the data to your image.

Here's a good place to start. When you can master this first, then you can write a better program to send images so they are intact on the receivers end.

VB.NET Socket Programming[^]
 
Share this answer
 
v2
Comments
Member 11450536 3-Aug-16 14:48pm    
The issue with UDP is that the datagram buffer size is too small to hold the image. With TCP, not all of the image gets through.
jkirkerx 3-Aug-16 16:55pm    
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.receivebuffersize(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900