这个
所以我像往常一样阅读流。然后它就卡住了。它不会抛出异常或任何东西。它只是卡住了。
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
我该怎么办才能使程序永远不会被卡住?注意我已将代码放在try catch块
中这是完整的程序。
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
答案 0 :(得分:1)
你的程序是否停留在第一个循环序列?
根据MSDN mabye,这应该有帮助
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
答案 1 :(得分:1)
Stream.Read 一直等到它预期的字节数,如果在程序正好在读取>时正在中断通信strong>并且连接关闭程序将永远在这一点上等待剩余的字节来,除非你将配置 ReadTimeout ,然后在此期间没有接收任何字节后它会抛出一个例外。 然后,您应该尝试捕获并重新启动连接。