Click here to Skip to main content
15,888,170 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,
I am trying to make a remote access tool where a client computer can be monitored from a server computer.
For getting the client's computer desktop view, my programme take screen shots, save the file and send the
file to remote server computer. Here is the main code for the client-

VB
Private Sub Live_pc()
        Dim cmnd As String
        Dim fs As FileStream
        Dim flag As Boolean = True
        Dim b As Bitmap = New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim gfx As Graphics = Graphics.FromImage(b)
        Dim filesize As Int64
        Dim buffer() As Byte

        Do
            cmnd = bReader.ReadString()

            Select Case cmnd
                Case "Auto_Capture"
                    While flag
                        gfx.CopyFromScreen(My.Computer.Screen.Bounds.X, My.Computer.Screen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
                        b.Save("temp.xxx", Imaging.ImageFormat.Jpeg)
                        fs = File.OpenRead("temp.xxx")
                        ReDim buffer(fs.Length - 1)
                        filesize = fs.Length
                        fs.Read(buffer, 0, fs.Length)
                        bWriter.Write(filesize)
                        bWriter.Write(buffer, 0, fs.Length)
                        fs.Close()
                        flag = bReader.ReadBoolean
                        Threading.Thread.Sleep(30)
                    End While
                Case "Stop_Auto_Capture"
                Case "exit"
                    b.Dispose()
                    fs.Dispose()
                    gfx.Dispose()
                    Array.Clear(buffer, 0, buffer.Length)
                    Exit Do

            End Select
        Loop
    End Sub

and the code for server which receive files-
VB
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Drawing
Imports System.Threading
Imports System.Net.Sockets


Public Class frmLivePC
    Private tcpclient As New TcpClient
    Private BR As BinaryReader
    Private BW As BinaryWriter
    Private CaptureFlag As Boolean

    Private Sub frmLivePC_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        BW.Write("exit")
    End Sub

        Public Sub New(ByVal client As TcpClient)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        tcpclient = client
        BR = New BinaryReader(tcpclient.GetStream())
        BW = New BinaryWriter(tcpclient.GetStream())
        ' Add any initialization after the InitializeComponent() call.

    End Sub

      Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        BW.Write("Auto_Capture")
        CaptureFlag = True
        btnStart.Enabled = False
        btnStop.Enabled = True

        Dim AutoCapturingThread As New Thread(AddressOf AutoCapture)
        AutoCapturingThread.Start()
    End Sub

    Private Sub AutoCapture()
        While CaptureFlag
            Dim fs As FileStream
            Dim filesize As Int64
            filesize = BR.ReadInt64
            Dim BUFFER_SIZE As Int32 = 4096
            Dim BUFFER(BUFFER_SIZE - 1) As Byte
            Dim byteread As Integer

            fs = File.Create("temp.jpg")

            While filesize
                If filesize < BUFFER_SIZE Then BUFFER_SIZE = filesize
                byteread = BR.Read(BUFFER, 0, BUFFER_SIZE)
                fs.Write(BUFFER, 0, byteread)
                filesize -= byteread
            End While
            fs.Close()
            PictureBox.ImageLocation = "temp.jpg"
            BW.Write(CaptureFlag)
        End While
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        CaptureFlag = False
        BW.Write(CaptureFlag)
    End Sub
End Class


I'm only providing the main part of the code. An active TCP connection is already made between server and client,
and the client starts the Live_pc() sub as the server starts frmLivePc Form. Now, my programme woks fine but having
some performance issue. Most of the times it hangs or the frames
are shown at a slow rate. I know i can get better performance if
i use UDP instead of TCP. But the problem is that the jpg files are around 110-140kb and i cant send these through
UDP. Using "UDPClient.send" method gives me a error telling that tthe datagram is too big to send over network.
Now i would like you to help me here. How can i get better performance? Is there any modification required or
do i have to use UDP and if so how would i do that? And i have a second question- the thing i'm trying to do here,
can it be done by forwarding the video memory of client's computer in the network? Some code about this would
be very helpfull. Thanks in advance :)
Posted

1 solution

Sending huge data can be done by chunks using UDP. Then you need to hash it, send it. But ther's no way to track if any chunk misses etc. All these are built-in TCP and you get it free. So I would not suggest modifying UDP transmission and ultimately reaching TCP.

You could use some webserver.

May be of some help:
Remote Desktop using C#.NET[^]

http://kishordgupta.wordpress.com/2011/02/03/controlling-pc-and-remote-access-via-lan-in-c/[^]

Thanks,

Kuthuparakkal
 
Share this answer
 
Comments
pdoxtader 11-Sep-12 14:07pm    
Agreed - UDP packets are not guaranteed to reach their destination. You should check out this project. It provides tcp server and client classes that make tcp communication much easier, and automates sending text, files and byte arrays.

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