Question Client/SSLstream - Waiting for the Response

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hey everyone.. I'm having a bit of an issue with my TCP Client/SSLstream where on occasion I will get a "reference is not set to an instance of an object" error. I've looked over my request and it doesn't seam to be missing anything, so I've come to believe this issue is coming from the response not being available since the server could still be processing it. When the code tries to look at the response, there is no object there and thus, the error is thrown..

I have a couple of individual subs written to Send my Request, Receive my Response, Convert Int32 to BigEndian and then convert it back to Int32. Is there anything I can add to the code (or adjustment that could be made) to make sure I'm not trying to read the response until I know it's available?

Thread Code
VB.NET:
    Public Sub RegisterDomain(ByVal domainName As String, ByVal thread As Integer)
        Dim Request As XElement
        Dim requestResponse As String = String.Empty

        Try
            Request = MY-XML-CODE-HERE

            'send the registration request
            SendRequest(Request.ToString, sslStream, System.Text.Encoding.UTF8)

            'retrieve the response
            requestResponse = GetResponse(sslStream, System.Text.Encoding.UTF8)

            'read the response
            If requestResponse.Contains("bla bla bla") = True Then
                txtLog.AppendText(domainName & " SUCCESSFUL at " & TimeOfDay & vbCrLf)
            ElseIf requestResponse.Contains("bla bla bla 2") = True Then
                txtLog.AppendText(domainName & " UNSUCCESSFUL at " & TimeOfDay & vbCrLf)
            Else
                txtLog.AppendText(domainName & " UNKNWON at " & TimeOfDay & vbCrLf)
                txtLog.AppendText(vbCrLf & requestResponse & vbCrLf)
            End If
        Catch ex As Exception
            txtLog.AppendText(domainName & " EXCEPTION: " & ex.Message & " at: " & TimeOfDay & vbCrLf)
        End Try
    End Sub

SendRequest
VB.NET:
    Private Sub SendRequest(ByVal msg As String, ByVal strm As System.Net.Security.SslStream, ByVal encoding As System.Text.Encoding)
        Dim ret As String = Nothing
        ' get encoded message bytes
        Dim msgBytes As Byte() = encoding.GetBytes(msg)

        ' get encoded message length in Big Endian, Windows is Little Endian
        ' this array will be 4 bytes long
        Dim totalMessageLength As Int32 = msgBytes.Length + 4 ' add 4 for the length preface
        Dim msgLengthBytes As Byte() = Int32AsBigEndianBytes(totalMessageLength)

        ' write message length
        strm.Write(msgLengthBytes, 0, msgLengthBytes.Length)

        ' write message
        strm.Write(msgBytes, 0, msgBytes.Length)
        strm.Flush()
    End Sub

GetResponse
VB.NET:
    Private Function GetResponse(ByVal strm As System.Net.Security.SslStream, ByVal encoding As System.Text.Encoding) As String
        Dim ret As String = Nothing
        Try
            ' get encoded response length in Big Endian, Windows is Little Endian
            ' this array will be 4 bytes long
            Dim msgLengthBytes(0 To 3) As Byte
            Dim readBytes As Int32 = strm.Read(msgLengthBytes, 0, 4)
            If readBytes = 4 Then
                ' should get back 4 bytes that are the BigEndian length
                Dim totalResponseLength As Int32 = BigEndianInt32BytesToInt32(msgLengthBytes)
                ' subtract the bytes that comprise the length
                Dim responseLength As Int32 = totalResponseLength - 4

                ' get encoded message bytes
                Dim msgBytes(0 To (responseLength - 1)) As Byte
                readBytes = strm.Read(msgBytes, 0, responseLength)
                ret = encoding.GetString(msgBytes)
            End If
            Return ret
        Catch ex As Exception
            Return ret
        End Try
    End Function

Thanks in advance!
 
on occasion I will get a "reference is not set to an instance of an object" error
You have not explained this, but I see in GetResponse you have Try-Catch and return Nothing if any exception, then in calling code you do requestResponse.Contains - requestResponse may be Nothing here.
 
Back
Top