Having some trouble with what I would think would be a fairly simple process.
I have a server. I want to verify that I can access that server on port 1433.
The function I am using is:
Public Shared Function ServerOnline(ByVal address As String, ByVal port As Integer) As Boolean
Dim timeout As Integer = 500
Dim result As Boolean = False
Try
Using socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim asyncResult As IAsyncResult = socket.BeginConnect(address, port, Nothing, Nothing)
result = asyncResult.AsyncWaitHandle.WaitOne(timeout, True)
socket.Close()
End Using
Return result
Catch
Return False
End Try
End Function
The result I am getting is: TRUE. No matter what. Even if I have pulled my machine completely off the network.
I suggest that after you WaitOne() on your IAsyncResult you need to check and report the value of socket.Connected. THAT’s what will tell you whether you got through to the server.
The WaitOne() call will return when the connection either unambiguously succeeds or fails or times out. You’re not bothering to see which of those three things happened. You’re effectively just assuming success.
Also you might want to call .ShutDown on the socket before letting it fall out of scope via the Using statement. It’s not clear .ShutDown() is necessary if you never launched a Send or Receive operation, but it’s also not clear it isn’t.
socket.Shutdown()
socket.Disconnect() 'Or the aysnc equivalent BeginDisconnect/EndDisconnect construction
Socket.Close() 'Not needed if your socket is declared via a Using statement.
I doubt the lack of a .Disconnect() call will affect how your *client *operates.
But it may cause open sockets to hang out on the *server *longer than you need them to. In other words, you’d be doing a very mild DOS attack on your server. Depending on how often you execute your connection checking, this may become a problem. In fact a server you don’t control may well decide you’re hostile & block you altogether.