How do I do a network communication project?

Source: Internet
Author: User

http://blog.csdn.net/clarkcc1988/article/details/8825106


JAVA TCP, Socket,mina




What is a socket


Two programs on the network realize the exchange of data through a two-way communication connection, one end of this two-way link is called a socket. The socket is typically used to implement a connection between the client and the service party. The socket is a very popular programming interface for the TCP/IP protocol, and a socket is uniquely determined by an IP address and a port number.


However, the type of protocol supported by the socket is not only TCP/IP, so there is no connection between the two. In the Java environment, socket programming mainly refers to the network programming based on TCP/IP protocol.





Short connection is a simple and effective way of processing, but also the most widely used. The question is which side breaks first.
Open the connection. Can be in the service side, but also in the client, but advocates active disconnect at the service end;








Summarize:
Server-side program or client program creation process:
Create a connection---?? Add Message Filter (encoding and decoding, etc.)--> add business Processing




Mina is in the middle tier, and it does not care about how the underlying network data is transmitted, only to receive the underlying data,
Filter and convert Java objects to our applications, then filter the application response values and
Converts to the bottom-identified byte and provides the underlying transmission;
------Anyway: Mina is the interface between the underlying data transfer and user application interaction.






About Remote communications ...


A complete, large software project often requires several vendor modules to complete.
How vendors communicate with each other. Need to customize the communication channel. Generally by the hardware manufacturer to develop the relevant channel.
(software vendors need to follow hardware vendors)


For example:
This time we do is the telematics project, our company is responsible for doing a gateway module, hardware manufacturers are depots, car machine reported instructions through three channels to request.
They were: sms,http,tcp. (Specific, details of the transmission mechanism here do not say)


The communication between the manufacturers requires the protocol, and then the vendors communicate with each other according to the prior custom protocol.


About Agreement formulation:
Before development, each vendor needs to meet to specify a set of protocols, that is, how to codec.
Why make an agreement? What are the common protocol formulation methods?
We know that the underlying transmission is binary data, the server and the client to establish a connection after the data interaction,
Accept the message sent by the other side, how to determine the request sent or the response data is over.
Can not always be silly waiting, or casual end of the message received it. This requires a rule.
For example, QQ Chat tool, when the input after a message, click the Send button to send to the other side, this time the department
The unified will add a text line break after your message, the receiver sees this text line break to think
This is a complete message, parsed into a string display. And this rule is called agreement.


Ways to develop an agreement:
1: Fixed-Length Message method: This method is to use the length of the data sent, generally applicable to the command hair
Send. For example: Data sent by the end of the data are two bytes, AA to start, BB table
Shows off and so on.


2: The word Fu Ding boundary method: This way is the use of special characters as data terminator, generally applicable to
The sending of simple data. For example: Automatically add text line breaks at the end of a message (Windows
Using \r\n,linux \ n), the receiving party sees the text line break as a complete
Message, ending the receive data to begin parsing. Note: The special character that ends the logo must be
Simple, often using special characters in the ASCII code to identify them.


3: Fixed-Length message head method: Using fixed-length message headers, in the message header in a domain to indicate the length of the message. The
The method is most flexible and most widely used. For example: Protocol-Protocol number (1 bytes) + Data length
Degree (4 bytes) + Real data. After the request arrives, resolve the protocol number and data length, root
According to the length of the data to determine whether the subsequent real data received intact. Message for HTTP protocol
The content-length in the header is also the length of the message body, so that the receiving end of the data
You'll never have to read the data until you've read the number of bytes.


Under the agreement,
Converting binary data to Java objects is called decoding (also known as unpacking);
Translating Java objects into binary data is called encoding (also known as packaging);
Here we will focus on the specific use of the following two protocols.




Author: Li Shaohua
Email: xiaosanshaoli@126.com
qq:305409913






About encoding and decoding:
The data transmitted by the network is binary data, and our program (JAVA) cannot directly manipulate binary data;
At this point we need to convert the received byte array to a string, which of course can be converted to any Java basic data type or object.
That's the decoding. The exact opposite of the encoding is the conversion of the string being transmitted to bytes.

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.