Recently, for a project, the interaction between C # and Java is required, that is, the C # Write server, and the Java write client to allow communication between the two.
Communication is nothing more than sending data to each other. socket technology is preferred. A persistent connection is established through the TCP protocol. Data is usually transmitted in the form of byte arrays. That is to say, no matter what data is transmitted, it is first split into byte arrays.
Since it is object-oriented programming, the data to be sent must be encapsulated by the entity class, and combined with list <> generic set, it can be very flexible and convenient.
To put it bluntly, the core issue of this article is: C # communicating with Java through passing objects?
At the beginning, I was very confused. I don't know if this can be implemented, but I was very excited because I was doing something interesting. No problem found after research and exploration! Feasible! Share the following information!
Core technologies required:
N socket.
N json.
The following describes the key steps:
Communication Key:
C # and Java with socket communication, send data and receive data can be unified using UTF-8 encoding, after testing, using UTF-8 encoding can successfully pass the object.
For the socket technology, before sending data, the string is generally converted into a byte array, and then sent to a byte array. When receiving data, the received data is also a byte array, which needs to be converted to a string in many cases.
Below are some common conversions.
C #Language string to UTF-8Byte array:
Byte [] B = encoding. utf8.getbytes ("123 ");
C #Language UTF-8Convert byte arrays to strings:
// Receivedata is a byte array and EN en is a byte array Length
String removemsg = encoding. utf8.getstring (receivedata, 0, en );
JavaLanguage string to UTF-8Byte array:
/* Strcontent is the string to be converted */
Byte [] BS = strcontent. getbytes ("UTF-8 ");
JavaLanguage UTF-8Convert byte arrays to strings:
/* Chararray is a byte array in UTF-8 format, which is usually specified as a UTF-8 when obtained from the IO stream, read_rst is a byte array length */
String resultstr = new string (chararray, 0, read_rst );
Serialization key:
As we all know, objects are stored in the memory, specifically the heap zone in the memory. Therefore, when we try to save and pass an object, we must first serialize the object into a character representation to make it invisible.
Because of cross-language interaction, we cannot use Java-specific serialization methods or C #-specific serialization methods. We must find a common serialization format to interact with each other. Obviously, JSON is the best choice.
We can see that JSON is required to complete the following functions:
After testing, use the following method to achieve the above conversion.
C # client can use the open source project JSON.. net.. Net version, and select the corresponding newtonsoft. JSON. DLL, reference and add: Using newtonsoft. JSON; using newtonsoft. JSON. converters.
C #JSONSerialization object method:
// Te is the object to be serialized; obj is the serialized string of the object.
String OBJ = jsonconvert. serializeobject (TE );
C #JSONDeserialization object method:
// Testentity is the target type; obj is the object serialized in JSON format.
Testentity TE = jsonconvert. deserializeobject <testentity> (OBJ );
Java can use the open-source project Google-gson. The downloaded package is in jar format. Import the package directly in the project and add reference: Import COM. google. gson. gson.
JavaJSONSerialization object method:
Gson = new gson ();
/* Te is the object to be serialized */
String S = gson. tojson (TE );
JavaJSONDeserialization object method:
Gson = new gson ();
/* S is the JSON serialized object, string type; testentity is the target type */
/* Note: when an object is deserialized using the fromjson method, the object type must be displayed to declare a constructor without parameters */
Testentity TE = gson. fromjson (S, testentity. Class );
This is the key part. It is very easy to implement in other places, and there are many matureCode.