Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Posted

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 haven't figured out how to close down existing connections from the server.

 

Any help on this would be appreciated.

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

  • 3 years later...
Posted

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

Anyway, i'm having the exact same prob as the original poster. I've been using the same microsoft example of multi-client connections.

 

The only problem I'm having is completely severing all connections. Destroying the looping listener thread is not an issue. I've 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 can't seem to shut down the connections fully w/out exiting the Server App.

I've tried almost every variation of this to sever the TCPListener Connection:

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?

I'll 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

   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)

   Public Sub CloseTcpListener()
       If LanTcpListenerWasStarted = True Then
           LanTcpListenerWasStarted = False

           LanTcpClient = Nothing
           LanTcpListener.Server.Close()

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

       End If

   End Sub

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
  • 6 months later...
Posted

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:

 

 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

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...