Application of serialization and deserialization techniques in writing socket applications

Source: Internet
Author: User
Tags serialization socket

When we're writing socket-related applications, when we're sending soft, complex data, it's possible that the most common thing we do is convert the data from parts to strings, and then connect the strings with a delimiter to send. However, I do not know whether you have thought that there is a problem.

For example, I use # to separate the strings, to look up the server-side based on what the client has entered, and then to return the result, in case of a # in the lookup keyword entered by the user, that will affect our segmentation of the string.

I wonder if you have ever thought that the technique of serialization and deserialization is also used on the socket? First defines a class that encapsulates the data, serializes the object of the class before sending it, and then sends it, receiving the byte stream and then deserializing it to get an object.

This is much better than sending strings alone.

Here's an example of server-side development with WPF, the client is the Windows Store app, and of course I'm just giving examples here, but this can be used for all types of applications, including Windows Phone apps, and the principle is the same.

One, server-side

First we define a class that encapsulates the data, this is demonstrated in a product information class, which is defined on both the server side and the client, to facilitate serialization and deserialization, and you can write to a class library and then both the server side and the client reference the class library.

[DataContract (Name = "Product")]     
public class product     
{     
    ///<summary>     
    ///Product number     
    ///</summary>     
    [DataMember (Name =] Product_no ")] public     
    string Productno {get; set;}     
    <summary>     
    ///Product name     
    ///</summary>     
    [DataMember (name = "Product_Name")]     
    public String ProductName {get; set;}     
    <summary>     
    ///Product Unit price     
    ///</summary>     
    [DataMember (Name = "Product_price")]     
    public Decimal Productprice {get; set;}     
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.