Networkcomms V3 using a JSON serializer for network communication

Source: Internet
Author: User

Just hanging out on the internet, accidentally saw an article

C # (server) and Java (client) pass objects through sockets

URL is: http://www.cnblogs.com/iyangyuan/archive/2012/12/23/2829712.html#3140522

It is mentioned that C # and Java, such as by passing object communication problems quote as follows:

Communication key: C # and Java with socket communication, send data and receive data can be unified using UTF-8 code, tested, using utf-8 encoding can successfully pass an object.         For socket technology, before sending data, the string is generally converted into a byte array, and then the byte array is sent, and when the data is received, it is also a byte array, many times it needs to be converted into a string.          Below are some common conversions. C # language string converted to UTF-8-byte array:byte[] B = Encoding.UTF8.GetBytes ("123"); C # language UTF-A 8-byte array is converted into a string://Receivedata is a byte array, Receiven is a byte array length         stringRemovemsg = Encoding.UTF8.GetString (Receivedata,0, Receiven); The Java language string is converted to UTF-8-byte array:/*Strcontent is the string to convert*/         byte[] bs = Strcontent.getbytes ("UTF-8"); Java language UTF-A 8-byte array is converted into a string:/*Chararray is an array of bytes in the UTF-8 format, typically obtained from the IO stream when specified as Utf-8,read_rst is the byte array length*/String ResultStr=NewString (Chararray,0, Read_rst); Serialization key: As you know, objects are stored in memory, specifically in the heap area in memory.         So when we try to save and pass the object, we must first serialize the object into a character representation and turn the intangible into tangible. Because it is a cross-lingual interaction, we cannot use Java-specific serialization or C #-specific serialization, and must find a common serialization format to interact with.         Clearly, JSON is the choice of the two.          As a result, we need the JSON to do the following: After testing, the above conversion can be achieved by the following methods. C # end can be json.net with open source project, after downloading according to own. NET version, select the appropriate Newtonsoft.Json.dll, reference and then add:usingNewtonsoft.json;usingNewtonsoft.Json.Converters, ready to use. C # Serializes object methods with JSON://TE is the object to serialize; obj is the string after the object is serialized         stringobj =jsonconvert.serializeobject (TE); C # Deserializes object methods with JSON://testentity is the target type; obj is a JSON-serialized object, in the form of a stringtestentity te= jsonconvert.deserializeobject<testentity>(obj); Java side can use open source project Google-Gson, after downloading is a jar format package, import this package directly in the project, and add the reference: Import Com.google.gson.Gson; ready to use. Java serialization of object methods using JSON: Gson Gson=NewGson (); /*TE is an object that needs to be serialized*/String S=Gson.tojson (TE); Java Deserializes object method with JSON: Gson Gson=NewGson (); /*S is a JSON-serialized object, string type; Testentity is the target type*/         /*Note: When you use the Fromjson method to deserialize an object, the type of the object must be displayed by declaring a constructor without parameters*/testentity te= Gson.fromjson (s,testentity.class);
Quote

Think of it. Networkcomms V3 with Jsonserializer serializer

The Jsonserializer in Networkcomms V3 is the packaging of the Newtonsoft.json, making it easier to use in Networkcomms V3 frames

Let's start with an example of how a C # client communicates with the C # server through JSON serialization.

A test was made to create a new project file as follows:

The user class in MessageContract is used to pass information

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacemessagecontract{ Public classUser { PublicUser () {} Public stringName {Get;Set; }  Public intAge {Get;Set; } }}

Resmessag class for storing returned information

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace messagecontract{   public  class  resmessage    {         public Resmessage () {}       publicstringgetset  ; }    }}

Client code:

New Sendreceiveoptions (dpsmanager.getdataserializer<jsonserializernullnull);
Messagecontract.user theuser =NewMessagecontract.user (); Theuser.name="Famous Mountains network Studio"; Theuser.age= -; Resmessage resmsg= Newtcpconnection.sendreceiveobject<user, resmessage> ("Requser","Resuser", the, Theuser, SRO, SRO); MessageBox.Show ("the information returned from the server is:"+resmsg.message);

Server-side code:

New Sendreceiveoptions (dpsmanager.getdataserializer<jsonserializernullnull);
  Networkcomms.appendglobalincomingpackethandler<user> ("requser", Incominguserhandle);
   Private voidIncominguserhandle (packetheader header, Connection Connection, User msg) {Try{resmessage Resmessage=NewResmessage (); Resmessage.message= Msg. Name +"you are welcome"; Connection. SendObject ("Resuser", Resmessage, SRO); }            Catch(Exception ex) {logtools.logexception (ex,"Incomingmsghandle"); }        }

The example runs successfully with the following screen:

Java for many years no, try Java to write an Android client to see if you can communicate with the C # writing server, if this is very good.

If there is wrong to write the place, Wan hope you point out, I good timely correction, or some netizens will also say waste his time:)

www.networkComms.cn Edit

Reference:

Http://www.cnblogs.com/leonwang/archive/2011/08/12/2136497.html

Http://www.cnblogs.com/wgp13x/p/0effafd9f5283cbf36e62b4fb5e94c81.html

Http://www.cnblogs.com/zhanghaomars/p/3557644.html

Networkcomms V3 using a JSON serializer for network communication

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.