By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Problems with TCP sockets and failing threads

P: n/a
I start up a thread which does this:

Sub ReaderThread

Dim tcpCli As New TcpClient

Try
tcpCli.Connect("100.21.0.5", 2101)
Catch
tcpCli.Close()
End Try

End Sub

This connects OK. If there is a problem, then tcpCli closes and the thread
terminates.
I have a separate ThreadMonitor on a Timer which triggers every 2 seconds.
If it detects that the above thread has failed it restarts it as follows:

Dim t1 As New Thread(New ThreadStart(AddressOf ReaderThread))
t1.Start()

The problem is that when I get to the line:

tcpCli.Connect("100.21.0.5", 2101)

It fails to connect, and ends up in the Catch section and terminates again.
If I start the program again, it connects OK. It's as though somehow a
resource is not being cleared.

However I can do

tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()
tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()
tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()
tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()

forever, and it works OK.

I'm mystified.

-Jerry
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Try throwing an exception and see why it's actually ending up in the Catch
section of the statement. I'm kind of curious why myself.

Try
tcpCli.Connect("100.21.0.5", 2101)
Catch ex as exception
tcpCli.Close
Messagebox.show(ex.message)
'or you could do:
'Throw New Exception(ex)
End Try

-Jason

"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42***********************@ptn-nntp-reader01.plus.net...
I start up a thread which does this:

Sub ReaderThread

Dim tcpCli As New TcpClient

Try
tcpCli.Connect("100.21.0.5", 2101)
Catch
tcpCli.Close()
End Try

End Sub

This connects OK. If there is a problem, then tcpCli closes and the thread
terminates.
I have a separate ThreadMonitor on a Timer which triggers every 2 seconds.
If it detects that the above thread has failed it restarts it as follows:

Dim t1 As New Thread(New ThreadStart(AddressOf ReaderThread))
t1.Start()

The problem is that when I get to the line:

tcpCli.Connect("100.21.0.5", 2101)

It fails to connect, and ends up in the Catch section and terminates
again. If I start the program again, it connects OK. It's as though
somehow a resource is not being cleared.

However I can do

tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()
tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()
tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()
tcpCli.Connect("100.21.0.5", 2101)
tcpCli.close()

forever, and it works OK.

I'm mystified.

-Jerry

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.