Mutual calls between JAVA and. NET -- basic architecture of mutual calls between TCP/IP (with the original code)

Source: Internet
Author: User

Concept of TCP/IP socket
TCP/IP (Transmission Control Protocol/Internet Protocol) is a communication protocol for network interconnection, through which various heterogeneous networks or heterogeneous machines can communicate with each other. TCP/IP is short for Transmission Control Protocol/Internet Protocol, this Protocol is the most basic protocol of the Internet and the foundation of the Internet's international interconnection network. Simply put, it is composed of the IP protocol at the network layer and the TCP protocol at the transmission layer. TCP/IP defines how electronic devices (such as computers) connect to the Internet and how data is transmitted between them. TCP/IP is a layer-4 architecture. The high-level layer is the transmission control protocol, which aggregates information or splits files into smaller packages. The lower layer is the Internet protocol, which processes the address section of each package so that these packages arrive at the destination correctly. TCP/IP has become the most mature and widely used Interconnection protocol in today's computer networks. The Internet uses the TCP/IP protocol. As long as the TCP/IP protocol is installed on various computers on the network, they can communicate with each other.

Development of TCP/IP socket communication

Among the many development languages, most development languages support TCP/IP communication, and the development process is very similar. First set the Socket, and then the client sends the request information, the server connects the client to receive the request and then returns the information. In the. NET System, the Socket object is encapsulated in the TcpClient object to manage the lifecycle of the Socket object. During the development process, the development languages of the server and the client are different. The server is developed in the JDK1.6 environment, but the client needs to use it.. NET development client, which may confuse developers! The following uses JAVA as the server and. NET as the client as an example to describe how to use the TCP/IP protocol to call each other between JAVA. NET. There are many examples such as TCP/IP Implementation of chat rooms, and the development is relatively simple, because both parties use String to transmit information. In the real Establishment of ERP, OA, CRM and other systems, both parties must first establish a unified communication contract to achieve TCP/IP communication, the following describes a typical Enterprise Information Communication instance.

 

Information Transmission Method


Because. NET and JAVA have different characteristics, it is impossible for both parties to directly transmit information through serialized objects. There are three common information exchange methods:

1. The most clumsy and complicated method of information transmission is to directly use the "header file description + field attribute" method. This is an original and troublesome communication method, because each contract must send a request in binary mode, even if it is the same type of contract, as the parameters are different, the length of each request also changes. Although this transmission method is troublesome, it is often seen when different development languages call each other. This may be because developers are not fully familiar with the two development languages, therefore, the most primitive and simple development method is put upside down.

 


 

2. XML is the most common and widely used information transmission method. XML is supported in most development platforms, So XML is the most common in Web network messaging. However, the biggest drawback of XML is that it is too good at understanding and consumes a lot of transmission traffic.

3. for the disadvantages of XML, JSON came into being and developed rapidly. JSON originally originated from Javascript and is mostly used only for B/S page development. However, with the development of technology and the support of multiple development languages, JSON can be viewed everywhere today. JSON not only provides a set of cross-platform communication methods, but also removes the need for complex XML features. It is welcomed by developers of all types.

 


Server Development

Communication contract

First, create a set of server and client to accept the communication Contract at the same time. The name feature of Contract is the Contract name, which is used by the server in Contracts. find the contract in the xml file, and find the package name, class name, call method, and other attributes of the contract based on the package attribute, class attribute, and method attribute of the output.


Contracts. XML
<Contracts> <Contract name = "GetPersonByAge"> // name indicates the Contract name, the server and client must comply with this contract at the same time <Input> <Description> obtain the People object set whose Age is equal to this value </Description> // describes the contract content </Input> <Output> <Package> Manager </Package> // name of the Package called when the GetPersonByAge request is received <Class> PersonManager </Class> // name of the Class called when the GetPersonByAge request is received <Method> GetListByAge </Method> // name of the processing Method called when the GetPersonByAge request is received </Output> </Contract> <Contract name = "GetPersonByID"> <Input> <Description> get the People object whose ID is equal to this value </Description> </Input> <Output> <Package> Manager </Package> <Class> PersonManager </Class> <Method> GetListByID </Method> </Output> </Contract> </Contracts>
Transmit information in JSON format

Although most of the current development in C/S still uses serialized objects and segmented fields for communication between the two parties, in this example, we will use the JSON communication method as an example. First, the client sends a request to the server using JSON In the rated format:

{"ContractName": "GetPeopleByAge", "Params": [23]}

The ContractName indicates the Contract Name. Based on this Name, the system finds the Contract item with Name equal to GetPeopleByAge in the Contracts. xml file. Find the corresponding Package, type, and Method in the Package, Class, and Method of the corresponding Output item.

Params is a parameter transmitted by the client. The server will call the object method and enter parameter 23 to obtain the calculation result. Finally, the result is returned to the client.

There are two points worth noting here. The first point is that the contract format in JSON is fixed, and both the server and client must comply with this contract, in ContractName, enter a required contract name, And in Params, enter a set of parameters, even if there is only one parameter. The second point is in Contracts. the Package, Class, and Method in the xml file and Output are customized on the server side. It is only bound to the server side to implement the GetPersonByAge contract Method, and these methods are not fixed, the server can be modified according to system requirements. This method is a bit like the Struts. xml file in Struts, which means that the server's processing method is separated from the request sent by the client.

Basic Structure
The basic structure of the system. The client sends a request to the server in JSON format {"ContractName": "GetPeopleByAge", "Params": [23, the server converts the received request to a Contract object using the "data transfer layer. Then, the logic conversion layer calls the corresponding method based on the Contract object and returns the calculation result to the client in JSON format.

Note that the JSON format is used during information exchange between the server and the client.

JSON Data Conversion
On the server side, after receiving a request from the client, the Transfer class is responsible for converting the received JSON data into a Contract object. In Transfer, the org. json toolkit is used as the JSON conversion tool. You can download the org. json toolkit from the following URL: html "> http://www.json.org/java/index.html.

The Implement Class contains the GetResult (Contract contract) Method, which calls the corresponding Method of the "logical transfer layer" based on the contract object Package, Class, Method, and other attributes, finally, return the calculation result to InputControl.

After receiving the request, the server directly calls InputControl to process the input data.
Code
// Contract entity class, including the Contract package, class, method, params, and other attributes package Model; import org. json. JSONArray; public class Contract {private String package1; private String class1; private String method; private JSONArray params; public void setPackage1 (String package1) {this. package1 = package1;} public String getPackage1 () {return package1;} public void setClass1 (String class1) {this. class1 = class1;} public String getClass1 () {return class1;} public void setMethod (String method) {this. method = method;} public String getMethod () {return method;} public void setParams (JSONArray params) {this. params = params;} public JSONArray getParams () {return params;} // converts the input String to a Contract object. // org. json Toolkit, as a conversion tool for JSON, org. the json Toolkit can download http://www.json.org/java/index.htmlpackage Common; import java. io. file; import java. io. IOException; import javax. xml. parsers. doc

Related Article

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.