Problems with TCP sockets and failing threads

  • Thread starter Thread starter Jerry Spence1
  • Start date Start date
J

Jerry Spence1

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
 
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
 
Back
Top