Sockets and Serialization

Hi,

I'm building a windows forms app that needs to connect to a server farm via the internet.
Performance is of the essence.

I'm thinking to use .NET remoting but I prefer sockets to avoid the complexity of remoting; the data I need to pass are small in size anyway. For larger data I'll use Web services.

I know that sockets connection can pass array of bytes and not objects.

Can I pass a serialized object from socket to socket There's any special consideration on doing this

Any help will be appreciated.

Thanks.

ubercoder



Answer this question

Sockets and Serialization

  • Matthew Adams9346

    Hi,

    Take a look at thread: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=72253&SiteID=1 this covers some of the issues discussed here.

    Sandor

    p.s. The current beta version of WCF aka Indigo has a 'go live' license so you can use it in a production senario.


  • P.J.Ganesh

    Blair,

    That's exactly what I want to do.. Use an object the same way remoting works but using sockets.

    The only reason for doing so is that I prefer sockets as a communication channel because is faster and easier to implement..


  • MariaW

    I can't use .Net 2.0 because most users don't have it in their Pc's and if is required they may not install my app...

    Have you ever used Genuine Channels. They have TCP bidirectional channels but I don't know if by using thei remoting transpot channels is the right choice.

    Does the HTTP repomintg channel supports bidir


  • Benjamin00

    I dont know is the answer but all I am saying is that is the way I am currently doing it - and is the first time I am doing it.

    obviously you need to open the appropriate ports on the firewall to establish a TCP connection otherwise it will fail there and then.



  • UntimelyWill

    Blair,

    Thanks for your reply.

    By complexity I mean the mechanics to buidl the interfaces, objects to be remoting enable.

    Remoting also is not bidirectional behind firewalls and NATs.

    For that reason I tried Guniune Channels, but things are complicated there too.

    To me sockets look much simpler.

    ubercoder.


  • Dean Forant

    what is meant by bi/di

  • sameera

    Thanks a lot.

    Did you find a way without the XML file


  • Desmond Molins

    Doug,

    Can I serialize and pass any object

    Can I run methods on the object after is passed to the client like in remoting

    Any links to code

    Thanks.


  • AmolWankhede

    "The only reason for doing so is that I prefer sockets as a communication channel because is faster and easier to implement.."

    Do you think there's a chance that your reasoning is bias'ed based on your experience with sockets and remoting Remoting is a built-in distributed technology that handles rich data structures - well - and the underlying communication is flexible and could be the same as sockets. I believe the "easier" argument is much better applied to remoting is "easier" than sockets because of the general abstraction and broad functionality. I'm ok w/ "faster" but then you could go the route of using unmanaged sockets and forego managed code entirely.

    Unless you need specific functionality such as UDP or multicast sockets, remoting is a very good choice.

    I'm not sure where this thread left off, but if you need additional remoting code snippets - just ask again and I'll see what I could do.


  • keby

    and this is different from serializing an object over remoting

    And this wouldnt be subject to the same NAT/Firewall problems as remoting



  • Shyne79

    arent your sockets going to run into the same thing with sockets behind firewalls/nat

    you can remote over http bi-di.

    derive your classes from marshalbyref and tag em with serializable, and they are remotable.



  • Henry Hahn - MSFT

    The answer to your question if I understand:

    you wish to serialize an object and pass it via a network stream (TCP/IP)

    yes you can. I am currently doing this.

     

    Make sure that both ends have the exact same class - the class of which you wish to serialize/deserialize

    Then to serialize:


    XmlSerializer serializer = new XmlSerializer(TypeOf(someClass));
    someClass t = new someClass();
    //set values/properties of this new class, t, or use an existing instance or even go through each item in the array and serialize if you wish.
    serializer.Serialize(
    this.theNetworkStream, t);
    this.theNetworkStream.Flush();

     

     

    That will send the serialized objects via the networkstream  - the network stream is a client stream, which has the stream used the communicate with the client.

    To deserialize:



    //recieve incoming data (remember data sent will be in bytes[] therefore you have to convert to string and store to an xml file
    public void DoDeserialize(string theXmlData)
    {
       StreamWriter theXmlDoc = new StreamWriter(Application.StartupPath + "\\serializedXml.xml", false);
    theXmlData = theXmlData.Replace("\r", String.Empty);
    theXmlData = theXmlData.Replace("\n", String.Empty);
    theXmlDoc.Write(theXmlData);
    theXmlDoc.Close();
     
    //read the saved xml file
    StreamReader sr = new StreamReader(Application.StartupPath + "\\serializedXml.xml");
    someClass t = new someClass();
    XmlSerializer s = new XmlSerializer(TypeOf(someClass));
    t = (someClass)s.Deserialize(sr);
    sr.Close();
     
    }

     

     

    That will deserialize the serialized data. I'm sure there is a better way (Which I am working on) without having to write the data to an XmlFile, then read it and deserialize it.

    I hope that helps!



  • Gboyega

    ubercoder wrote:
    I'm thinking to use .NET remoting but I prefer sockets to avoid the complexity of remoting; the data I need to pass are small in size anyway. For larger data I'll use Web services.
    What do you mean by complexity Coding or the underpinnings Remoting is actually quite straight forward, especially for simple data interchange.

    In most simple cases, remoting is just a matter of specifying activation type and uri. Get your object model right and it is remotable with one line of code and some simple config entries.

    Get Rammer's book!



  • Forseti

    it means that you can use the same tcp connection for duplex communication.

    answering the first question now...

    if you want to use sockets, have fun.

    you can use the binaryformatter to serialize a CLR object to bytes and then ship them over the socket.

    simple to do...

  • Sockets and Serialization