I'm pretty new to serialization so please bear with me.
I want two instances of my application to communicate with each other over the internet. I have successfully rigged up a TCP client/server relationship and used a binary formatter to get the two sides to swap a single pair of messages. Here's the client side...
using (TcpClient clientSocket = new TcpClient(ipAddress, currentPort))
{
    using (NetworkStream stream = clientSocket.GetStream())
    {
        // send
        bformatter.Serialize(stream, new Message());
        // recv
        return (Message)bformatter.Deserialize(stream);
    }
}
It's cool, but not very useful for an application that needs to send messages in response to user events. So I need to be able to send and receive asynchronously.
I basically want an interface that behaves like this:
class BidirectionalObjectStream
{
    public BidirectionalObjectStream(TcpClient client)
    {
        //...
    }
    // objects go in here
    public void SendObject(object o)
    {
        //...
    }
    // objects come out here
    public event Action<object> ObjectReceived;
}
Is there a class like this that's part of .NET?  If not, how should I implement the receive event?  Maybe a dedicated thread calling bformatter.Deserialize() repeatedly...?
Any help appreciated.
The question is a little broad.
I can think of two options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With