"Stop Server" button on a Multi-user threaded server

mooman_fl

Well-known member
Joined
Nov 3, 2002
Messages
193
Location
Florida, USA
Hola everyone.... been away for a while but I am back now.

Working on a new project now... working on a server based off of the Multi-user Chat program off of the Microsoft site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet08282001.asp

I understand a bit of it but I am new to both network programming AND threading. The problem is that the server in the example makes a Client object for each client connecting to the server and runs each object in a separate thread. They have a way of passing events out of the thread to the main process... but I want to be able to disconnect all clients and stop the server from listening without shutting down the app. I would also like to have a list of clients connected where I can boot individual connected clients.

I may be mistaken but I getting the impression that you can pass events out of the thread to the main process but not vice-versa... Any ideas how I might accomplish my goal? As a bit of background I am trying to convert the SocketServer project from the above link into a plugin for my app.... so this would have to work from a plugin DLL.
 
Ok... after going back through this code I was able to easily stop the TCPListener and close down the thread that listens for new connections. However I havent figured out how to close down existing connections from the server.

Any help on this would be appreciated.
 
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:
LanTcpClient = Nothing
LanTcpThread.Abort()
LanTcpListener.Server.Close()
LanTcpListener.Stop()
LanTcpListener.Close

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:
    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

This is the function trying to close all connections completely (Not working)
Code:
    Public Sub CloseTcpListener()
        If LanTcpListenerWasStarted = True Then
            LanTcpListenerWasStarted = False

            LanTcpClient = Nothing
            LanTcpListener.Server.Close()

            LanTcpListener.Stop()
            LanTcpThread.Abort()
            LanTcpListener = Nothing

        End If

    End Sub
 
This post was still on the first page of the Network section :)

The prob we both ended up having with Microsofts Multi-user chat server example was to shut down the server and all connections w/out exiting the server program.

This can be done by making sure each and every TCPClient in the Client Collection is shut down. In this MS example, TCPListener gets a new tcpclient and places it in a collection, so to shut down all connections shutting down the listener alone wont work. Something like this does:

Code:
  Public Sub CloseAllConnections()
        Try

            If LanTcpListener IsNot Nothing Then
                LanTcpListener.Server.Close()
                LanTcpListener.Stop()
            End If

            SyncLock ClientCollection
                For i As Integer = ClientCollection.Count - 1 To 0 Step -1
                    If ClientCollection(i).mobjClient IsNot Nothing AndAlso ClientCollection(i).mobjClient.Connected = True Then
                        ClientCollection(i).mobjClient.GetStream.Close()
                    End If
                    If ClientCollection(i).mobjClient IsNot Nothing Then
                        ClientCollection(i).mobjClient.Close()
                    End If
                Next
            End SyncLock

        Catch ex As Exception
            Console.WriteLine("Exception: {0}", ex)
        End Try

    End Sub

Note: I think orignially they used a hash table for the collection.. I changed it to a Generic List
 
Back
Top