Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need help to connect my client server application.
The client is supposed to capture the screen and send to server.
It's like teamviewer but simple one.
I'm using vb.net, sockets and serialization library.

On the server, I declare two thread.
VB
Dim listening As New Thread(AddressOf listen)
Dim getimage As New Thread(AddressOf receiveimage)

This is the listen method which I use as thread.
VB
Private Sub listen()
    Dim bf As New BinaryFormatter

    While 1 = 1
        server.Start()
        client = server.AcceptTcpClient
        ns = client.GetStream
        PictureBox1.Image = bf.Deserialize(ns)
    End While
End Sub

This is the receive method which also a thread.
VB
Private Sub receiveimage()
    Dim bf As New BinaryFormatter

    While 1 = 1
        ns = client.GetStream
        PictureBox1.Image = bf.Deserialize(ns)
    End While
End Sub

And this is the form load event.
VB
Private Sub viewing_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    port = Integer.Parse(Form1.TextBox1.Text)
    server = New TcpListener(Net.IPAddress.Parse("192.168.1.104"), port)

    listening.Start()
    client = New TcpClient("michael", port)
    getimage.Start()
End Sub

Now, on the client.
I declare three variables.
VB
Dim client As New TcpClient
Dim ns As NetworkStream
Dim port As Integer

And I use this function to get the screenshot and this works.
VB
Public Function dp() As Image
    Dim bounds As Rectangle = Nothing
    Dim screenshots As System.Drawing.Bitmap = Nothing
    Dim graph As Graphics = Nothing
    bounds = Screen.PrimaryScreen.Bounds
    screenshots = New Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(screenshots)
    graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    Me.PictureBox1.Image = screenshots
    Return screenshots
End Function

And this method is to send the screenshot.
VB
Private Sub senddp()
    Dim bf As New BinaryFormatter
    ns = client.GetStream
    bf.Serialize(ns, dp())
    Me.PictureBox1.Image = bf.Deserialize(ns) '< HERE	
End Sub

First, I use the method below to connect, then I use timer to periodically call senddp() to send the screenshot.
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    port = Integer.Parse(TextBox2.Text)
    Try
        client.Connect(TextBox1.Text, port)
        MsgBox("Connected")
    Catch ex As Exception
        MsgBox("failed")
    End Try
End Sub

The problem is, bf.deserialize(ns) takes too long to execute, and it never works.
The application looks like freezing.
I was able to connect to server, but I don't understand what I did wrong here.
Any help is appreciated ! :)
Posted
Updated 2-May-15 20:31pm
v2

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