|
-
Jun 27th, 2012, 10:05 AM
#1
Thread Starter
Junior Member
[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
-
Jun 27th, 2012, 10:11 AM
#2
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?
-
Jun 27th, 2012, 11:17 AM
#3
Thread Starter
Junior Member
Re: Multi-Threaded TCP Server - threads do not close...
 Originally Posted by Evil_Giraffe
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?...
-
Jun 27th, 2012, 11:29 AM
#4
Addicted Member
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 :|
-
Jun 27th, 2012, 01:14 PM
#5
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.
-
Jun 27th, 2012, 02:41 PM
#6
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|