JAVA communication series 1: Java Socket Technology Summary, logging ocket
This document describes how to learn about java Socket.
1 Socket communication Principle 1.1 ISO layer-7 Model
1.2 layer-5 TCP/IP Model
The application layer is equivalent to the Session Layer, presentation layer, and application layer in OSI.
Difference reference: http://blog.chinaunix.net/uid-22166872-id-3716751.html
1.3 TCP Packets
(1) sequence number: Seq number, which occupies 32 bits. It is used to identify the byte stream sent from the TCP source end to the target end. It is marked when the initiator sends data.
(2) Check serial number: Ack serial number, which occupies 32 bits. The check result is valid only when ACK flag is 1. Ack = Seq + 1.
(3) Signs: there are 6 in total, including URG, ACK, PSH, RST, SYN, and FIN. The meanings are as follows:
(A) URG: urgent pointer is valid.
(B) ACK: Check whether the serial number is valid.
(C) PSH: the receiver should send the packet to the application layer as soon as possible.
(D) RST: reset the connection.
(E) SYN: initiate a new connection.
(F) FIN: releases a connection.
Note that:
(A) Do not confuse Ack with ACK in the flag.
(B) the validator Ack = the initiator Req + 1 and the two ends are paired.
1.4 Socket communication
Socket is an intermediate software abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a group of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. for users, a set of simple interfaces are all, let the Socket organize the data to conform to the specified protocol.
1.5 three-way handshake
Socket Connection established and closed, see: http://www.2cto.com/net/201310/251896.html
2 basic communication concepts 2.1 short connections
Connection> data transmission> close connection
A short connection refers to a SOCKET connection that sends data and immediately disconnects after receiving the data.
For example:
HTTP1.0 is short connection by default and stateless. Each HTTP operation is performed by the browser and server, a connection is established, but the connection is interrupted when the task ends. ,
Http1.1 is a persistent connection by default
Stateless: the Protocol has no memory for transaction processing;
2.2 persistent connections
Connection> data transmission> keep connection> data transmission>... -> Close the connection.
After a SOCKET connection is established, the connection is consistent regardless of whether the connection is used or not.
2.3 Half-Outsourcing
The recipient did not receive a complete package and only accepted part of it;
Cause: In order to improve the transmission efficiency, TCP allocates a large package so that the receiver cannot accept the package at one time.
Impact: both persistent connections and short connections may occur.
2.4 stick package
When receiving data from multiple packages sent by the sender to the receiver, it is pasted into a package. From the receiving buffer, the header of the next packet is followed by the end of the previous packet.
Classification: one is that all the packets that are attached together are complete data packets, and the other is that the packets that are attached together have incomplete packets.
There are many reasons for the phenomenon of sticking packets:
1) sender sticks to the packet: Due to the TCP protocol itself, to improve transmission efficiency, the sender often needs to collect enough data before sending a packet of data. If few data is sent several times in a row, TCP will merge the data into a packet based on the optimization algorithm and send the data once. In this way, the receiver receives the data in the sticky packet.
2) receiver stick package: the user process of the receiver does not receive data in a timely manner, leading to stick packets. This is because the receiver puts the received data in the system receiving buffer, and the user process obtains data from the buffer. If the data of the previous packet has not been taken away by the user process when the next packet arrives, then, when the next packet of data is placed in the system's receiving buffer zone, it is received after the previous packet of data, and the user process obtains data from the system's receiving buffer zone according to the preset buffer size, in this way, multiple packets of data are obtained at a time.
2.5 Subcontracting
Subcontracting (1): When a stick package occurs, our receiver must subcontract the package;
Subcontracting (2): a data packet is divided into multiple recipients;
Cause: 1. result of IP address multipart transmission; 2. half a packet due to the loss of some packets during transmission; 3. A package may be divided into two transfers. when data is retrieved, a portion is obtained first (or it may be related to the size of the received buffer ).
Impact: both packages and packages will appear in persistent connections.
2.6 how to solve the half-pack and stick-to-pack Problems
The phenomenon of sticking packets and half-packets occurs because only the concept of stream exists in TCP and there is no concept of package.
UDP does not have half-packet or stick packets. The reason is that UDP is a complete data packet and is not merged during sending. Therefore, there is no stick packet during receiving.
Fixed Length: Data with a fixed length is sent each time;
Special identifier: Press enter and line feed are used as special identifiers. When a specified identifier is obtained, the package is complete.
Byte Length: The Protocol format of packet header, packet length, and packet body. It is complete only when the server obtains the specified packet length;
Reference article (illustrated): http://blog.csdn.net/pi9nc/article/details/17165171
2.7 When should I consider sticking packets?
Transient connection: avoid sticking packets;
The sending data has no structure, such as file transmission. In this way, the sender only sends the data, while the receiver only receives the data, and does not need to consider sticking packets;
Persistent connection: send data of different structures within a period of time after the connection;
Processing Method: the receiver creates a preprocessing thread, processes the received data packets, and separates the encapsulated packets;
2.8 Differences Between TCP and UDP
- Based on connection and no connection;
- Requirements on system resources (more TCP and less UDP );
- The structure of the UDP program is relatively simple;
- Stream mode and datagram mode;
- TCP ensures data correctness, UDP may cause packet loss, TCP ensures data order, and UDP does not.
- Network differentiation: the machine itself is good, but communication problems occur;
- Network jitter: latency in the network refers to the delay time when information is sent to and received, which is generally composed of transmission delay and processing delay. jitter refers to the time difference between the maximum delay and the minimum delay, for example, if the maximum latency is 20 ms and the minimum latency is 5 ms, the network jitter is 15 ms, which mainly identifies the stability of a network.
2.9 Other communication problems 2.10 references
A packet has no fixed length, and the Ethernet is limited to 1500 bytes. Is the MTU of the Ethernet. If the packet exceeds this value, TCP sets an offset for IP datagram for multipart transmission, currently, an 8 k (NTFS series) buffer can be set on the application layer. 8 k of data is split from the underlying layer, and the application only sends data once.
For UDP, it should not be too large, generally between 1024 and 10 K. Note that, no matter how large a package is, the IP layer and the link layer will send your package in parts. Generally, the LAN is about 1500, And the Wan is only several dozen bytes. The fragmented package will arrive at the receiver through different routes. For UDP, if one of the parts is lost, the IP layer of the receiver will discard the entire packet, which leads to packet loss.
As a stream, the packet sending process does not reach the entire package, but is continuously sent, so the receiver must group packets. UDP, as a message or datagram, must arrive at the receiver in the whole package.
Generally, packet sending has packet boundary. The first thing you need to do is to let the receiver know the length of the packet, so there is a packet header information. For TCP, the receiver first receives the packet information, then, collect the package data. You can collect the entire package at a time. You can verify whether the result is collected. This completes the group package process. UDP, you can only receive the entire package. If the Received Buffer you provide is too small, TCP returns the actual length of the received Buffer, and the rest can be received. The difference between UDP is that the remaining data is discarded and the WSAEMSGSIZE error is returned. Note TCP: If the Buffer buffers you provide are large, you may receive multiple sending packets, and you must separate them. When the Buffer is too small, you cannot close the data in the Socket at a time, therefore, the Socket receives the event (OnReceive) and may not be triggered again.
3 Socket instance 3.1 Socket Communication Model
3.2 complete Socket Architecture Model
- Solution: half-pack, stick-to-pack (codec)
- Multithreading supported by the server
- Supports heartbeat Detection
- Specify a port to instantiate a SeverSocket
- Call the ServerSocket accept () method to block the connection while waiting
- Obtain the stream of the underlying Socket for read/write operations.
- Encapsulate data into a stream
- Read and Write Socket
- Close the opened stream
3.3 server development
// Create a ServerSocket object and listen to the bound Port
ServerSocket server = new ServerSocket (1000 );
// Establish a receiving Socket to block the response
Socket client = server. accept ();
// Obtain the input stream for receiving client information
InputStream in = server. getInputStream ();
// Obtain the output stream for outputting client information
// Wrap the input stream for ease of use
BufferedReader inRead = new BufferedReader (new InputStreamReader (in ));
// Wrap the output stream for ease of use
PrintWriter outWriter = new PrintWriter (out );
While (true)
{
String str = inRead. readLine (); // read
OutWriter. println ("output to client ");
OutWriter. flush (); // output
If (str. equals ("end "))
{
Break;
}
}
// Close the Socket
Client. close ();
Server. close ();
3.4 client development
- Instantiate a Socket through IP addresses and ports and request to connect to the server
- Obtain the stream on the Socket for read/write
- Encapsulate the stream into an instance of BufferedReader/PrintWriter
- Read and Write Socket
- Close the opened stream
// Use Socket to establish a connection with the server
Socket client = new Socket ("127.0.0.1", 1000 );
InputStream in = client. getInputStream (); // get the input stream
OutputStream out = client. getOutputStream (); // get the output stream
// Package the input stream and output stream
BufferedReader inRead = new BufferedReader (new InputStreamReader (in ));
PrintWriter outWriter = new PrintWriter (out );
// Obtain console input
BufferedReader inConsole = new BufferedReader (new InputStreamReader (in ));
While (true)
{
String str = inConsole. readLine (); // read console input
OutWriter. println (str); // output to the server
OutWriter. flush (); // refresh the buffer
If (str. equals ("end "))
{
Break;
} // Exit
System. out. println (inRead. readLine () // read server output
}
Client. close ();
Running the client in DOS
Java-classpath sockettest-0.0.1-SNAPSHOT.jar (cn.com. gome. sockettest. basic. ClientTest)
3.5 multi-thread Server
3.6 heartbeat Detection
Method 1: socket. sendUrgentData (0); // sends 1-byte emergency data. By default, the server does not enable emergency data processing and does not affect normal communication.
Try {
Socket. sendUrgentData (0xFF );
} Catch (Exception ex ){
Reconnect ();
}
This byte is automatically discarded as long as the SO_OOBINLINE attribute of the Socket is not enabled, and the SO_OOBINLINE attribute is disabled by default.
Method 2: Custom heartbeat string
3.7 various types of data read/write
Transmission byte, character, object [ObjectInputStream ObjectOutputStream] (Instance)