Hi, I said a long time ago that it wasn't receiving packets in order... here is the case...
This is my modified MessageClient.Read:
[highlight=vb] Private Sub Read(ByVal ar As IAsyncResult)
Try
'The stream will be Nothing if the client has been disposed.
If Me.stream IsNot Nothing Then
Dim buffer = DirectCast(ar.AsyncState, Byte())
'Complete the asynchronous read and get the first block of data.
Dim byteCount = Me.stream.EndRead(ar)
If byteCount = 0 Then
'If there is no data when an asynchronous read completes it is because the server closed the connection.
Me.OnConnectionClosed(New ConnectionEventArgs(Me.server))
Else
'Start building the message.
'Dim message As New StringBuilder(Me.Encoding.GetString(buffer, 0, byteCount))
OnDataChunkRecieved(New DataChunkRecievedEventArgs() With {.Chunk = buffer})
'As long as there is more data...
While Me.stream.DataAvailable
'...read another block of data.
byteCount = Me.stream.Read(buffer, 0, Me.BufferSize)
OnDataChunkRecieved(New DataChunkRecievedEventArgs() With {.Chunk = buffer})
End While
'Listen asynchronously for another incoming message.
Me.stream.BeginRead(buffer, 0, Me.BufferSize, AddressOf Read, buffer)
'Notify any listeners that a message was received.
'Me.OnMessageReceived(New MessageReceivedEventArgs(Me.server, message.ToString()))
End If
End If
Catch ex As IOException
'The callback specified when BeginRead was called may get invoked one last time when the TcpClient is disposed.
'This exception is thrown when EndRead is called on a disposed client stream.
End Try
End Sub[/highlight]
I will try to describe this the best way i can ... but basically ... when there is no more data available in the Me.stream.DataAvailable loop ... because we have reached the end of the stream that the client has received sofar (but it is still sending such as a large file).. it will then come back to this sub again as the server is still sending ... and Dim buffer = DirectCast(ar.AsyncState, Byte()) then starts reading data from a random chunk... not necessarily the next chunk that was sent...
also ... if i change the code to this it works:
Code:
[FONT=Courier New][COLOR="#A9A9A9"] While Me.stream.DataAvailable
'...read another block of data.
byteCount = Me.stream.Read(buffer, 0, Me.BufferSize)
OnDataChunkRecieved(New DataChunkRecievedEventArgs() With {.Chunk = buffer})
[COLOR="#B22222"][B]'wait for a second to see if more data is coming...
System.Threading.Thread.Sleep(100)[/B][/COLOR]
End While[/COLOR][/FONT]
As this seems to make it slow down enough to receive the next chunk ... but this is not good of course as it slows it down ... and the data may take more than 100ms to get the next bit... it is just proof of concept...
Any ideas jmcilhinney?
Thanks,
Kris