F
FLASHCODR
Guest
Hello,
I have a goal of send and receive screenshots continuously using Asyncronous socket and created this following code after see several references on web. Before try send continuously i tested sending only one time, but screenshot not is received correctly on server.
Some idea about how fix it? Thanks in advance.
Server:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class frmServer
Dim serverSocket As Socket
Dim clientSocket As Socket
Dim byteData(1023) As Byte
Private Sub FrmServer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim IpEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 8800)
serverSocket.Bind(IpEndPoint)
serverSocket.Listen(5)
serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
End Sub
Private Sub OnAccept(ByVal ar As IAsyncResult)
clientSocket = serverSocket.EndAccept(ar)
'New connection established
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), clientSocket)
serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
End Sub
Private Sub OnReceive(ByVal ar As IAsyncResult)
Dim client As Socket = ar.AsyncState
client.EndReceive(ar)
'Using MStream As New MemoryStream(byteData)
'Dim returnImage As Image = Image.FromStream(MStream)
'pbDesktop.Image = returnImage
'End Using
File.WriteAllBytes(Environment.CurrentDirectory & "\screenshot.jpg", byteData)
'Array.Clear(byteData, 0, byteData.Length)
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), clientSocket)
End Sub
Client:
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Public Class frmClient
Private Function BytesScreen() As Byte()
Dim bounds As Rectangle
Dim screenshot As Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New Bitmap(bounds.Width, bounds.Height)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Dim ms As New MemoryStream()
screenshot.Save(ms, ImageFormat.Jpeg)
Dim bitmapbytestream(ms.Length) As Byte
bitmapbytestream = ms.ToArray
Return bitmapbytestream
End Function
Dim clientSocket As Socket
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim IpAddress As IPAddress = IPAddress.Parse("127.0.0.1")
Dim IpEndPoint As IPEndPoint = New IPEndPoint(IpAddress, 8800)
clientSocket.BeginConnect(IpEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)
End Sub
Private Sub OnConnect(ByVal ar As IAsyncResult)
clientSocket.EndConnect(ar)
Send(BytesScreen, clientSocket)
End Sub
Private Sub Send(ByVal byteData As Byte(), ByVal client As Socket)
Try
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
Catch ex As Exception
If Not client.Connected Then 'if connection was forcibly disconnected
'reconnecting to the server
btnConnect.PerformClick()
End If
End Try
End Sub
Private Sub OnSend(ByVal ar As IAsyncResult)
Dim client As Socket = ar.AsyncState
client.EndSend(ar)
'Send(BytesScreen, clientSocket)
End Sub
End Class
Continue reading...
I have a goal of send and receive screenshots continuously using Asyncronous socket and created this following code after see several references on web. Before try send continuously i tested sending only one time, but screenshot not is received correctly on server.
Some idea about how fix it? Thanks in advance.
Server:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class frmServer
Dim serverSocket As Socket
Dim clientSocket As Socket
Dim byteData(1023) As Byte
Private Sub FrmServer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim IpEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 8800)
serverSocket.Bind(IpEndPoint)
serverSocket.Listen(5)
serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
End Sub
Private Sub OnAccept(ByVal ar As IAsyncResult)
clientSocket = serverSocket.EndAccept(ar)
'New connection established
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), clientSocket)
serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
End Sub
Private Sub OnReceive(ByVal ar As IAsyncResult)
Dim client As Socket = ar.AsyncState
client.EndReceive(ar)
'Using MStream As New MemoryStream(byteData)
'Dim returnImage As Image = Image.FromStream(MStream)
'pbDesktop.Image = returnImage
'End Using
File.WriteAllBytes(Environment.CurrentDirectory & "\screenshot.jpg", byteData)
'Array.Clear(byteData, 0, byteData.Length)
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), clientSocket)
End Sub
Client:
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Public Class frmClient
Private Function BytesScreen() As Byte()
Dim bounds As Rectangle
Dim screenshot As Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New Bitmap(bounds.Width, bounds.Height)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Dim ms As New MemoryStream()
screenshot.Save(ms, ImageFormat.Jpeg)
Dim bitmapbytestream(ms.Length) As Byte
bitmapbytestream = ms.ToArray
Return bitmapbytestream
End Function
Dim clientSocket As Socket
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim IpAddress As IPAddress = IPAddress.Parse("127.0.0.1")
Dim IpEndPoint As IPEndPoint = New IPEndPoint(IpAddress, 8800)
clientSocket.BeginConnect(IpEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)
End Sub
Private Sub OnConnect(ByVal ar As IAsyncResult)
clientSocket.EndConnect(ar)
Send(BytesScreen, clientSocket)
End Sub
Private Sub Send(ByVal byteData As Byte(), ByVal client As Socket)
Try
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
Catch ex As Exception
If Not client.Connected Then 'if connection was forcibly disconnected
'reconnecting to the server
btnConnect.PerformClick()
End If
End Try
End Sub
Private Sub OnSend(ByVal ar As IAsyncResult)
Dim client As Socket = ar.AsyncState
client.EndSend(ar)
'Send(BytesScreen, clientSocket)
End Sub
End Class
Continue reading...