Parte del cuando abro la opción de escritorio remoto y la cierro después no soy capaz de volver abrirla

cde serv:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Drawing
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form2
    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()
        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()
        On Error Resume Next
        While client.Connected = False
            server.Start()
            client = server.AcceptTcpClient
        End While
        getImage.Start()

    End Sub
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        listening.Start()
    End Sub
    Private Sub Form2_closing(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        If client.Connected Then
            client.Close()
        End If
    End Sub

End Class
code cliente:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Drawing
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form1
    Dim client1 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 SendImage()
        Try
            Dim bf As New BinaryFormatter
            nstream = client1.GetStream
            bf.Serialize(nstream, Desktop())


            'Application.Restart()
        Catch ex As Exception
            Dim clientsList As New List(Of TcpClient)

            For Each client1 As TcpClient In clientsList

                nstream.Dispose()
                nstream.Close()
                clientsList.Remove(client1)
                client1.Close()
                Application.DoEvents()

            Next
        End Try
    End Sub


    Private Sub connect()

        Try
            client1.Connect("127.0.0.1", 8085)
        Catch ex As Exception
            Thread.Sleep(3000)
        End Try
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If client1.Connected Then
            SendImage()
        Else

            'client1.Close()
            'nstream.Close()
            'nstream.Dispose()
            connect()
            '            reconnect()
        End If
    End Sub


---pues eso que no conseige cerrar bien las conexiones o no consigo reconectar en caso de que se cierre e escritorio
Responder

Volver a “VB/.NET”