TCP Connection and DesktopView-VBForums
Results 1 to 4 of 4

Thread: TCP Connection and DesktopView

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    2

    TCP Connection and DesktopView

    Hi guys,
    i have a desktop viewer but i need to close the tcp connection and connect to other.
    How can i do this?

    Here is my codes:

    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
     Dim client As New TcpClient
     Dim nstream As NetworkStream
    
    Public Function Desktop() As Image
            Dim bounds As Rectangle = Nothing
            Dim screenshot As System.Drawing.Bitmap = Nothing
            Dim graph As Graphics = Nothing
            bounds = Screen.PrimaryScreen.Bounds
            screenshot = New Bitmap(bounds.Width, bounds.Height,        System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            graph = Graphics.FromImage(screenshot)
            graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
            Return screenshot
        End Function
    
        Private Sub ResimGönder()
            Dim bf As New BinaryFormatter
            nstream = client.GetStream
            bf.Serialize(nstream, Desktop())
        End Sub
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Dim client As New TcpClient
        Dim server As New TcpListener(8085)
        Dim nstream As NetworkStream
        Dim Listening As New Thread(AddressOf Listen)
        Dim getImage As New Thread(AddressOf receiveImage)
    
    Private Sub receiveImage() ' gelen ekran görüntüsünü PictureBox a yansıtıyoruz
            On Error Resume Next
            Dim bf As New BinaryFormatter
            While client.Connected = True
                nstream = client.GetStream
                PictureBox1.Image = bf.Deserialize(nstream)
            End While
        End Sub
    
        Private Sub Listen()
            While client.Connected = False
                server.Start()
                client = server.AcceptTcpClient
            End While
            getImage.Start()
        End Sub
    Thanks

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,651

    Re: TCP Connection and DesktopView

    Well, I assume the first code block is the client, but I don't see where you connect to the Server or who calls ResimGönder().
    Which side do you want to initiate the disconnect?
    I assume if you had something in the code on the client side that we can't see that would not call ResimGönder() but instead close the connection, then on the server side your While loop in receiveImage might exit.
    If the while loop exits then it seems you should be able to start another instance of the Listening thread before you exit the receiveImage() sub/thread.
    Code:
        Private Sub receiveImage()  ' ....
    '...
            End While
            Listening = New Thread(AddressOf Listen)
            Listening.Start()
        End Sub
    You could then start another client on another machine to connect to the server which should be listening.

    The way I've done it in the past (on the server side, with a single connection) is have a nested loop and only one thread.
    The thread would enter the outer loop and Listen for the connection.
    Once a connection was made it would enter the nested (inner) loop and continually read and process the stream.
    This inner loop was wrapped inside an exception handler.
    If the connection closed normally, or broke for some other reason, causing an exception, the inner loop would exit, the current connection closed, and the outer loop would loop back and we would be back listening for a new connection.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    2

    Re: TCP Connection and DesktopView

    Oh sorry.

    client:
    Code:
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
     Dim client As New TcpClient
     Dim nstream As NetworkStream
    
    Public Function Desktop() As Image
            Dim bounds As Rectangle = Nothing
            Dim screenshot As System.Drawing.Bitmap = Nothing
            Dim graph As Graphics = Nothing
            bounds = Screen.PrimaryScreen.Bounds
            screenshot = New Bitmap(bounds.Width, bounds.Height,        System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            graph = Graphics.FromImage(screenshot)
            graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
            Return screenshot
        End Function
    
        Private Sub ResimGönder()
            Dim bf As New BinaryFormatter
            nstream = client.GetStream
            bf.Serialize(nstream, Desktop())
        End Sub
    
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                client.Connect("127.0.0.1", "80")
                MsgBox("client connected!", vbInformation, "")
            Catch ex As Exception
                MsgBox("failed to connect!", vbInformation, "")
            End Try
        End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            timer1.start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            ResimGönder()
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            client.Close()
            Timer1.Stop()
        End Sub
    Server:
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Dim client As New TcpClient
        Dim server As New TcpListener(8085)
        Dim nstream As NetworkStream
        Dim Listening As New Thread(AddressOf Listen)
        Dim getImage As New Thread(AddressOf receiveImage)
    
    Private Sub receiveImage() ' gelen ekran görüntüsünü PictureBox a yansıtıyoruz
            On Error Resume Next
            Dim bf As New BinaryFormatter
            While client.Connected = True
                nstream = client.GetStream
                PictureBox1.Image = bf.Deserialize(nstream)
            End While
        End Sub
    
        Private Sub Listen()
            While client.Connected = False
                server.Start()
                client = server.AcceptTcpClient
            End While
            getImage.Start()
        End Sub
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            listener = New TcpListener(80)
            Listening.Start()
        End Sub
    I tried like you said ; "End of Stream encountered before parsing was completed."

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,651

    Re: TCP Connection and DesktopView

    I wasn't even aware the "On Error Resume Next" was even available in .Net because I've never used it.
    That isn't handling the exception, it is ignoring it.
    I was thinking a Try Catch like you have in other parts of your code.

    Since you need to restart, i.e. recreate threads and objects, rather than create some thread or objects in the declarations, they should be declared in the code before use so that when you restart that portion of code it can create new instances of the object or threads.
    I made a couple of changes in the code because they were flagged because I always have Option Strict on by default.
    I went ahead and recreated the projects with your code so I could test the changes.
    This works, but feels a bit awkward, but I'm not going to try to suggest more than I have already. So below is just my changes to your existing code work for me, along the lines of what I was thinking in my previous post.
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class ClientForm
      Dim Client As TcpClient
      Dim nstream As NetworkStream
    
      Public Function Desktop() As Image
        Dim bounds As Rectangle = Nothing
        Dim screenshot As System.Drawing.Bitmap = Nothing
        Dim graph As Graphics = Nothing
        bounds = Screen.PrimaryScreen.Bounds
        screenshot = New Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        graph = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
        Return screenshot
      End Function
    
      Private Sub ResimGönder()
        Dim bf As New BinaryFormatter
        nstream = Client.GetStream
        bf.Serialize(nstream, Desktop())
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
          Client = New TcpClient
          Client.Connect("127.0.0.1", 8085)
          MsgBox("client connected!", vbInformation, "")
        Catch ex As Exception
          MsgBox("failed to connect!", vbInformation, "")
        End Try
      End Sub
      Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Timer1.Interval = 250
        Timer1.Start()
      End Sub
    
      Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        ResimGönder()
      End Sub
    
      Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Client.Close()
        Timer1.Stop()
      End Sub
    End Class
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class ServerForm
      Dim client As New TcpClient
      Dim server As New TcpListener(8085)
      Dim nstream As NetworkStream
      Dim Listening As New Thread(AddressOf Listen)
      Dim getImage As Thread
    
      Private Sub receiveImage() ' gelen ekran görüntüsünü PictureBox a yansıtıyoruz
        Dim bf As New BinaryFormatter
        Try
          While client.Connected = True
            nstream = client.GetStream
            PictureBox1.Image = CType(bf.Deserialize(nstream), Image)
          End While
        Catch ex As Exception
        Finally
          If nstream IsNot Nothing Then nstream.Dispose()
          client.Close()
        End Try
        Listening = New Thread(AddressOf Listen)
        '  server.Start()
        Listening.Start()
      End Sub
    
      Private Sub Listen()
        While client.Connected = False
          server.Start()
          client = server.AcceptTcpClient
        End While
        getImage = New Thread(AddressOf receiveImage)
        getImage.Start()
      End Sub
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Listening.Start()
      End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.