Hi, I want to send/receive a serializable object in socket:
==========sender==========
Dim MyMessage As New MessageClass.Message()
Dim client As New TcpClient
client.Connect(My.MySettings.Default.Server,
My.MySettings.Default.ServerPort)
Dim clientStream As NetworkStream = client.GetStream
Dim serializer As IFormatter = New BinaryFormatter()
serializer.Serialize(clientStream, MyMessage)
clientStream.Close()
===========receive==========
Dim serializer As IFormatter = New BinaryFormatter()
Dim listener As TcpListener = New TcpListener(IPAddress.Any,
My.MySettings.Default.Port)
listener.Start()
Dim client As TcpClient = listener.AcceptTcpClient
Dim clientStream As New NetworkStream(client.Client, False)
Dim ReciveMessage As MessageClass.Message =
TryCast(serializer.Deserialize(clientStream2), MessageClass.Message)
MsgBox(ReciveMessage.ToString)
===============================
there are no problem. But when I put the receive function to a while
loop. I found that I can't get the second object. It shows some
exception.(in java, I can use Stream.flush; Stream.reset to fix it, but
I can't find the same in .net). Anyone can tell me
how to send/receive a serializable object in socket? (I want to write a
chat server/client, any other way?)
Thanks in advance.