Reply to thread

This is one old thread to bring back up, but thats what the Search Function promotes.

Anyway, im having the exact same prob as the original poster. Ive been using the same microsoft example of multi-client connections.


The only problem Im having is completely severing all connections. Destroying the looping listener thread is not an issue. Ive configured my client application to detect when the connection has been shut down. For some reason it only detects as shut-down when the Server App is completely closed. I cant seem to shut down the connections fully w/out exiting the Server App.

Ive tried almost every variation of this to sever the TCPListener Connection:

[code=vb]

LanTcpClient = Nothing

LanTcpThread.Abort()

LanTcpListener.Server.Close()

LanTcpListener.Stop()

LanTcpListener.Close

          

[/code]


There has to be some resource that is being disconnected or released when the Server App shuts down that I cant emulate programmatically. Any Ideas?

Ill include some of my other code. Much of it is work in progress after changing and still changing stuff so some code may appear useless.


This is how the listener is started and maintained

[code=vb]

    Public Sub StartTcpListener(ByVal port As Integer)

        PortNumber = port

        LanTcpThread = New Thread(AddressOf TcpListenThread)

        LanTcpThread.Start()

    End Sub



    Public Sub TcpListenThread()

        Try

            LanTcpListener = New TcpListener(IPAddress.Any, PortNumber)

            LanTcpListener.Start()

            LanTcpListenerWasStarted = True

            MF.InvokeTBoxAppendText(MF.txtStatus, "Waiting for Client on Port #" + PortNumber.ToString + "..." + vbCrLf)

               

           

                LanTcpClient = New Client(LanTcpListener.AcceptTcpClient) This acutally waits till a client is available

                AddHandler LanTcpClient.Connected, AddressOf OnConnected

                AddHandler LanTcpClient.Disconnected, AddressOf OnDisconnected

                AddHandler LanTcpClient.LineReceived, AddressOf OnLineReceived

               MF.InvokeTBoxAppendText(MF.txtStatus, "Connected Client on Port #" + PortNumber.ToString + vbCrLf)

           

        Catch


        End Try

    End Sub


    Private Sub OnConnected(ByVal sender As Client)

        MF.InvokeTBoxAppendText(MF.txtStatus, "Connected Client on Port #" + PortNumber.ToString + vbCrLf)

    End Sub


    Private Sub OnDisconnected(ByVal sender As Client)

        MF.InvokeTBoxAppendText(MF.txtStatus, "Disconnected Client on Port #" + PortNumber.ToString + vbCrLf)

        LanTcpClient = Nothing

    End Sub


[/code]


This is the function trying to close all connections completely (Not working)

[code=vb]

    Public Sub CloseTcpListener()

        If LanTcpListenerWasStarted = True Then

            LanTcpListenerWasStarted = False


            LanTcpClient = Nothing

            LanTcpListener.Server.Close()


            LanTcpListener.Stop()

            LanTcpThread.Abort()

            LanTcpListener = Nothing


        End If


    End Sub

[/code]


Back
Top