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.
Dim listening As New Thread(AddressOf listen)
Dim getimage As New Thread(AddressOf receiveimage)
This is the listen method which I use as thread.
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.
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.
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.
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.
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.
Private Sub senddp()
Dim bf As New BinaryFormatter
ns = client.GetStream
bf.Serialize(ns, dp())
Me.PictureBox1.Image = bf.Deserialize(ns)
End Sub
First, I use the method below to connect, then I use timer to periodically call senddp() to send the screenshot.
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 ! :)