Private Sub Receiving(ByVal iAr As IAsyncResult)
Console.WriteLine("Receiving callback started" + vbNewLine)
Try
SyncLock client.GetStream
Try
client.GetStream.EndRead(iAr)
Catch
Console.WriteLine("exciting")
Exit Sub
End Try
End SyncLock
Catch ex As Exception
Console.WriteLine("exciting")
Exit Sub
End Try
Dim sReader As StreamReader
Dim nChar As Integer
Dim StrBuffer(4096) As Char
SyncLock client.GetStream
sReader = New StreamReader(client.GetStream)
Try
nChar = sReader.Read(StrBuffer, 0, bByte.Length)
Catch ex As Exception
Console.WriteLine(ex)
End Try
Console.WriteLine(client.GetStream.CanRead)
newStr = New String(StrBuffer, 0, nChar)
Console.WriteLine(newStr)
Console.WriteLine("Receiving callback callbacked" + vbNewLine)
client.GetStream.BeginRead(bByte, 0, 4096, AddressOf Receiving, Nothing)
End SyncLock
End Sub
这是我的代码,我已经调试了一个小时,我找不到问题,因此我找不到解决方案
接下来应该发生的是接收方法得到一个字节数组,在调试时数组不是空的我已经检查过了,但是看起来有问题的地方就是它读取的行nChar = sReader.Read(StrBuffer, 0, bByte.Length)
在这一行调试只是停止无所事事,如果我删除该行继续功能,但问题是在这一行,我真的不知道是什么原因造成的。
如果您需要更多信息,请问我,我对此感谢非常困惑
答案 0 :(得分:1)
执行EndRead()时,收到的数据在字节数组“bByte”中已经。 EndRead()返回的值将是读取的字节数。您正尝试使用StreamReader再次阅读下面的内容,但在此之前您已经在缓冲区中获取了数据。
我不确定你是如何编码数据的,但通常你会在接收方做更多这样的事情:
Private Sub Receiving(ByVal iAr As IAsyncResult)
Console.WriteLine("Receiving callback started" + vbNewLine)
Dim nChar As Integer = client.GetStream.EndRead(iAr)
Dim newStr As String = Encoding.ASCII.GetString(bByte, 0, nChar)
Console.WriteLine(newStr)
Console.WriteLine("Receiving callback callbacked" + vbNewLine)
client.GetStream.BeginRead(bByte, 0, bByte.Length, New AsyncCallback(AddressOf Receiving), Nothing)
End Sub