I have a TCP Client socket that connects to a server when Form1 loads. I need a way for the client socket to "give up" trying to connect to the remote server if it takes too long to connect. i.e Time out after 30 seconds.
Currently when the socket cannot connect to the server for what ever reason the form will appear to freeze up until the server can be reached.
Its probably worth noting im using System.Net.Sockets.TcpClient()
Here is My Sub for creating the connection:
Public serverStream As NetworkStream
Public clientSocket As New System.Net.Sockets.TcpClient()
Public Sub CreateTCPConnection()
Try
clientSocket.Connect(System.Net.IPAddress.Parse(My.Settings.ServerIP), My.Settings.ServerPort)
ConnectionStatusLbl.Text = "Connection: Connected"
ConnectionStatusPB.Image = My.Resources.LED_Green
'This should work according to MSDN but does not
clientSocket.SendTimeout = 3000
clientSocket.ReceiveTimeout = 3000
UpdateTimer.Start()
Catch ex As Exception
ConnectionStatusLbl.Text = "Connection: Not Connected"
ConnectionStatusPB.Image = My.Resources.LED_Red
clientSocket.Close()
MsgBox(ex.ToString)
End Try
End Sub
BeginConnectmethod to perform the connection asynchronously. Full usage shown here - A Friend