TCP Client Exception Problem

tymbow

Newcomer
Joined
Dec 14, 2003
Messages
13
I've been looking into using sockets for a few web based test applets. I've cobbled a quick piece of test code together as follows to get a feel for how .NET works, however it is not behaving as I would expect with regards to exceptions.

When szHost is a valid or invalid FQDN or a valid IP address the function performs as expected . However if an invalid IP address is supplied (that is the host does not exist) I would expect the Connect method to throw a SocketException but it never does. It just continues on as if it successfully established a connection which it could not possibly have done.

I must be missing something obvious because this seems like it should be trivial. I've mucked around with the different ways of using Connect (ipEndPoint etc.) but the result is the same. Any thoughts?


Code:
Public Function TcpSocketTest(ByVal szHost As String, ByVal nPort As Int32, ByRef szResponseData As String, ByRef szResponseBytes As Int32) As Boolean

        TcpSocketTest = False
        szResponseData = String.Empty
        szResponseBytes = 0

        Try

            Dim objTcpClient As New TcpClient
            Dim objNetworkStream As NetworkStream
            Dim bData = New Byte(1024) {}
            Dim nBytes As Int32

            objTcpClient.Connect(szHost, nPort)

            objNetworkStream = objTcpClient.GetStream()

            If objNetworkStream.DataAvailable Then

                nBytes = objNetworkStream.Read(bData, 0, bData.Length)

                szResponseData = System.Text.Encoding.ASCII.GetString(bData, 0, nBytes)

            End If

            objTcpClient.Close()

            TcpSocketTest = True

            szResponseBytes = nBytes

            objNetworkStream = Nothing
            objTcpClient = Nothing

        Catch e As ArgumentNullException

            szResponseData = e.Message.ToString()

        Catch e As SocketException

            szResponseData = e.Message.ToString()

        End Try

    End Function
 
When using the synchronous Connect method of the Socket class the first thing you should do afterwards is check the Connected property to see whether or not it was successful. As you're using TcpClient instead of Socket, I'm not sure what to advise.

TcpClient appears to just call the Connect method of the underlying Socket, but doesn't check Connected afterwards. You could do this yourself, but the underlying Socket is only exposed via a protected member so you'd have to inherit to add your own Connected property.

Unfortunately I don't have much experience with TcpClient (I've always used Socket directly) so this is all I can suggest.
 
Hmmm..

I did try a similar thing with the Socket class and if I call the Connected method of the socket it returns true when it could not be as the IP address used to create the socket was not valid.

I might try to find some more example code and test it to see how it reacts to an invalid IP address.
 
Back
Top