user4951
user4951

Reputation: 33090

My program "stuck in this line" reading stream

This

So I am reading a stream like usual. Then it just stuck. It doesn't throw exception or anything. It just stucks.

            Try
                Do
                    read = stream.Read(datain, 0, datain.Length) 'Stuck here
                    responseData = System.Text.Encoding.ASCII.GetString(datain, 0, read)
                    finalResponse = finalResponse + responseData
                Loop While read > 0
            Catch ex As Exception

What should I do so that the program would never get stuck like that? Notice I already put the code inside try catch block

Here is the full program.

  Public Function getWhoisInfoAsText4(strDomainName As String, whoisServerToTry As String) As String
        'testAsisWhois()
        Dim data = System.Text.Encoding.ASCII.GetBytes(strDomainName & Microsoft.VisualBasic.vbCr & Microsoft.VisualBasic.vbLf)
        Dim finalResponse = String.Empty
        Dim client As TcpClient
        Try
            client = New TcpClient(whoisServerToTry, 43)
        Catch ex As Exception
            Return ""
        End Try

        Dim stream = client.GetStream()
        Using stream

            stream.Write(data, 0, data.Length)

            Dim datain = New Byte(254) {}
            Dim responseData = String.Empty
            Dim read As Integer
            Try
                Do
                    read = stream.Read(datain, 0, datain.Length)
                    responseData = System.Text.Encoding.ASCII.GetString(datain, 0, read)
                    finalResponse = finalResponse + responseData
                Loop While read > 0
            Catch ex As Exception

            End Try

        End Using

        If finalResponse.Contains("Name Server:") Then
            appendToTextFile(whoisServerToTry + vbNewLine, "appendfiles", Scripting.IOMode.ForAppending)
        End If

        finalResponse += (whoisServerUsed + whoisServerToTry + vbNewLine)
        Return finalResponse
    End Function

Upvotes: 0

Views: 330

Answers (2)

ManuVR
ManuVR

Reputation: 46

Stream.Read waits always until the amount of bytes it is expecting is coming, if there is an interruption on the communication just in the moment the program is exactly on the Read and the connection get close the program will be forever in this point waiting for the rest of the bytes to come unless you will configure a ReadTimeout, then after not receiving any bytes during this time it will throw an exception. You should then make a try catch and restart the connection.

Upvotes: 1

Stefan
Stefan

Reputation: 528

does your program stuck at the first loop sequence?

According to MSDN mabye this should help

            Dim numBytesRead as Integer = 0 
            Do
                read = stream.Read(datain, numBytesRead, datain.Length)
                responseData = System.Text.Encoding.ASCII.GetString(datain, 0, read)
                finalResponse = finalResponse + responseData
                numBytesRead += read
            Loop While read > 0

Upvotes: 1

Related Questions