Ok I have written this little Class that accepts a tcpclient as part of its constructor.
The write method serialises an object into a memorystream. It writes the length of the memorystream as 4 bytes. It then writes the contents of the memory stream.Code:Public Class OperatorPositionClass Private Client As System.Net.Sockets.TcpClient Private NetStream As Net.Sockets.NetworkStream Private MyStateMessage As StateMessage Public Event Receive(ByVal sender As OperatorPositionClass, ByVal Message As Object) Public Sub New(ByVal ConnectedClient As System.Net.Sockets.TcpClient) Client = ConnectedClient NetStream = Client.GetStream MyStateMessage = New StateMessage MyStateMessage.NetStream = NetStream NetStream.BeginRead(MyStateMessage.Bytes, 0, 4, New AsyncCallback(AddressOf AsyncReadCallback), MyStateMessage) End Sub Public Sub Write(ByVal Message As Object) Dim Ms As IO.MemoryStream = New IO.MemoryStream Dim Bf As Runtime.Serialization.Formatters.Binary.BinaryFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter Dim Bytes() As Byte Dim NumberOfBytes As Integer Try Bf.Serialize(Ms, Message) NumberOfBytes = CInt(Ms.Length) Bytes = BitConverter.GetBytes(NumberOfBytes) NetStream.Write(Bytes, 0, Bytes.Length) NetStream.Write(Ms.GetBuffer, 0, Ms.GetBuffer.Length) Catch ex As Exception MsgBox(ex.Message.ToString) End Try End Sub Private Sub AsyncReadCallback(ByVal ar As IAsyncResult) Dim Bf As Runtime.Serialization.Formatters.Binary.BinaryFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter Dim NumberOfBytesToRead As Integer Try Dim State As StateMessage = CType(ar.AsyncState, OperatorPositionClass.StateMessage) Dim NumberOfBytes As Integer = State.NetStream.EndRead(ar) If NumberOfBytes = 0 Then 'Raise an event to notify lost client Exit Sub Else NumberOfBytesToRead = BitConverter.ToInt32(State.Bytes, 0) Dim Ms As IO.MemoryStream = New IO.MemoryStream Dim ByteBuffer(1024) As Byte Dim Offset As Integer = 0 While NumberOfBytesToRead > 0 NumberOfBytes = State.NetStream.Read(ByteBuffer, 0, 1024) If NumberOfBytes = 0 Then ' Raise Event to notify Lost Client Exit Sub End If Ms.Write(ByteBuffer, Offset, NumberOfBytes) NumberOfBytesToRead -= NumberOfBytes Offset += NumberOfBytes End While Dim MyObject As Object = Bf.Deserialize(Ms) RaiseEvent Receive(Me, MyObject) End If NetStream.BeginRead(MyStateMessage.Bytes, 0, 4, New AsyncCallback(AddressOf AsyncReadCallback), MyStateMessage) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Class StateMessage Public NetStream As System.Net.Sockets.NetworkStream Public Bytes(3) As Byte End Class End Class
AsyncReadCallback blocks until data is available. It reads the first 4 bytes and converts it back to an integer. It then proceeds to read that amount of bytes into a memory stream. I then deserialise it back to an object.
Now for example if the memorystream in the write method has 58 bytes I see 58 bytes in the AsyncReadCallback memorystream.
The problem is when I try to deserialise it I get the following error message.
Which leads me to believe Im not getting all the bytes. But Im at a loss as what to do next.Code:System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed. at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) at OperatorPositionClass.AsyncReadCallback(IAsyncResult ar) in C:\Users\Adrian\Documents\Visual Studio 2010\Projects\Server Class\Server Class\OperatorPositionClass.vb:line 56
Just to Clarify I use this Class in both my Client and server app.
Help please.




Reply With Quote