Results 1 to 6 of 6

Thread: [RESOLVED] Multi-Threaded TCP Server - threads do not close...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Resolved [RESOLVED] Multi-Threaded TCP Server - threads do not close...

    Hello,

    Below is the portion of my TCP server. The problem I am experiencing is that threads are not closed properly. My understanding was that after the given Sub completes, then the thread closes. In my program for some reason about 30% of the threads created, are never being closed. I am looking at the Task Manager and they just keep going up.

    Can someone look over at my code and see if I am making any obvious mistakes? How can I find out and kill old/inactive threads?

    Thanks!

    Code:
    Private Sub StartTCPListenerMultiThreaded()
    	Dim localAddr As IPAddress = IPAddress.Parse(ip)
    	TCPserver = New Sockets.TcpListener(localAddr, listeningPort)
    	TCPserver.Start()			
    	While True
    		If TCPserver.Pending Then
    			Dim t As Thread
    			t = New Thread(AddressOf Me.acceptingThread)
    			t.Start()
    		End If
    		System.Threading.Thread.Sleep(20)
    	End While
    End Sub
    
    Private Sub acceptingThread()
    	Dim client As TcpClient = TCPserver.AcceptTcpClient
    	Dim stream As NetworkStream = client.GetStream
    	client.ReceiveTimeout = 90000
    	client.SendTimeout = 90000
    	
    	While client.Connected
    		Dim bytes(1) As Byte
    		Dim i As Int32
    			i = stream.Read(bytes, 0, bytes.Length)
    			While (i <> 0)
    				'do something here with received payload
    				If stream.DataAvailable = False Then
    					Exit While
    				End If
    				i = stream.Read(bytes, 0, bytes.Length)
    			End While	
    	End While
    	
    End Sub

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Multi-Threaded TCP Server - threads do not close...

    My first thought would be that whilst you exit the outer while loop when the client is no longer connected, that probably happens while you are waiting to read a byte from the socket. Since the client has disconnected, it is never going to send that byte, and so the thread is stuck on the Read call for ever?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Re: Multi-Threaded TCP Server - threads do not close...

    Quote Originally Posted by Evil_Giraffe View Post
    My first thought would be that whilst you exit the outer while loop when the client is no longer connected, that probably happens while you are waiting to read a byte from the socket. Since the client has disconnected, it is never going to send that byte, and so the thread is stuck on the Read call for ever?
    Hmmm... that might be it.
    When I debug the program stops on this line:

    i = stream.Read(bytes, 0, bytes.Length)

    If the socket is open, I am waiting for an incoming message. But let's say the client disconnects - then I have no way of knowing that.
    How can I timeout this thread?

    I've read in so many places that aborting threads is a bad idea. I just don't know how to kill all of those threads with those broken connections.

    Most examples I've seen on-line are using the above stream.read... can anyone suggest some other way?...

  4. #4
    Addicted Member cellus205's Avatar
    Join Date
    Dec 2010
    Location
    San Antonio, TX
    Posts
    237

    Re: Multi-Threaded TCP Server - threads do not close...

    Sorry I can't help, just wanted to stop by and say awesome name. My name is the same and it's not one you see too often, lol. Maybe you are me in another dimension :|

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Multi-Threaded TCP Server - threads do not close...

    Use BeginRead instead. Its asynchronous and hence wouldn't block. However, you will have to make that sub a little more complicated. The fact that it requires a callback procedure for receiving data is one complication. I think its worth it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Re: Multi-Threaded TCP Server - threads do not close...

    I found the problem.
    Code:
    While client.Connected
    Above line was causing this loop to continue even when the client disconnected!
    I just added a safety check - simple timeout which breaks the while loop if no data is transmitted over 2 hours (which works perfect in my case).

    Thanks everyone for help!

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width