PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET How to send/receive a serializable object in socket?

Reply

How to send/receive a serializable object in socket?

 
Thread Tools Rate Thread
Old 13-12-2006, 02:05 PM   #1
ChopDown
Guest
 
Posts: n/a
Default How to send/receive a serializable object in socket?


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.

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off