views:

38

answers:

4

Hello!

i have to send/recieve objects(of a custom class made by me) in my c# .net 4.0 app and i would like a good tutorial to get me started because i've searched on google and there seem to be a lot of problems with serialization/ deserialization and although the problems we're solved, there are a lot of ugly hacks .

Regards, Alexandru Badescu

+1  A: 

If you have control over the objects you could decorate them with the [Serializable] attribute and use BinaryFormatter for serialization/deserialization.

Darin Dimitrov
+1  A: 

For TCP/IP communication I highly recommend Stephen Cleary's FAQ, you should pay special attention to Message Framing. You might also want to take a look at his NitoSockets implementation.

All that assuming you can't just use WCF, of course.

ohadsc
A: 

If you talking about how to use tcpclient via socket programming in

there is a sample written by jgauffin and it referenced to

which both of them are very useful, if you see the question this helps didn't solved my problem but helped me to have a good and simple test cases (i have it before with more complexity).

Also refrences provided by A_Nablsi are usefull to have a good sample.

But if you talking about WCF service :D there is nothing.

SaeedAlg
+1  A: 

I've made a transport that does just this:

http://fadd.codeplex.com/SourceControl/changeset/view/58859#1054822

The library is work in progress, but the BinaryTransport works. It will also attempt to reconnect if it get disconnected.

Example:

public class Example
{
    private BinaryTransport<Packet> _client;
    private ServerExample _server;

    public void Run()
    {
        // start server
        _server = new ServerExample();
        _server.Start(new IPEndPoint(IPAddress.Loopback, 1234));

        // start client
        _client = new BinaryTransport<Packet>(new IPEndPoint(IPAddress.Loopback, 1234));

        // send stuff from client to server
        _client.Send("Hello world!");

        // send custom object
        _client.Send(new User { FirstName = "Jonas", LastName = "Gauffin" });
    }
}


public class ServerExample
{
    private readonly List<BinaryTransport<Packet>> _clients = new List<BinaryTransport<Packet>>();
    private SimpleServer _server;

    private void OnClientAccepted(Socket socket)
    {
        var client = new BinaryTransport<Packet>(socket);
        client.Disconnected += OnDisconnected;
        client.ObjectReceived += OnObject;

        _clients.Add(client);
    }

    private void OnDisconnected(object sender, EventArgs e)
    {
        var transport = (BinaryTransport<Packet>) sender;
        transport.Disconnected -= OnDisconnected;
        transport.ObjectReceived -= OnObject;
    }

    private void OnObject(object sender, ObjectEventArgs<Packet> e)
    {
        Console.WriteLine("We received: " + e.Object.Value);
    }

    public void Start(IPEndPoint listenAddress)
    {
        _server = new SimpleServer(listenAddress, OnClientAccepted);
        _server.Start(5);
    }
}

[Serializable]
public class Packet
{
    public object Value { get; set; }
}

[Serializable]
public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
jgauffin
Hi, I referenced to you
SaeedAlg
I saw that, thanks =) The BinaryTransport is another class in the library though.
jgauffin