This article is to learn the Java socket collation data for reference.
1 Socket Communication principle1.1 ISO seven-layer model
1.2 TCP/IP five layer model
The application layer is equivalent to the session layer in the OSI, the presentation layer, the application layer.
Difference Reference: http://blog.chinaunix.net/uid-22166872-id-3716751.html
1.3 TCP Messages
(1) Serial number: SEQ sequence number, which is 32 bits, is used to identify the byte stream sent from the TCP source end to the destination, which is flagged when the initiator sends the data.
(2) Confirm the serial number: ACK number, accounting for 32 bits, only the ACK mark bit is 1 o'clock, confirm the ordinal field is valid, ack=seq+1.
(3) Sign: A total of 6, namely Urg, ACK, PSH, RST, SYN, FIN, etc., the specific meaning is as follows:
(A) URG: The emergency pointer (urgent pointer) is effective.
(B) ACK: Confirm the serial number is valid.
(C) PSH: The receiving Party should submit this message to the application layer as soon as possible.
(D) RST: Resets the connection.
(E) SYN: Initiates a new connection.
(F) FIN: Release a connection.
It is important to note that:
(A) Do not confuse the ACK ordinal ack with the acknowledgment in the flag.
(B) Confirmation Party ack= Initiator req+1, paired on both ends.
1.4 Socket Communication
A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.
1.5 Three-time handshake
Socket connection setup and shutdown, see: http://www.2cto.com/net/201310/251896.html
2 Basic concepts of communication2.1 Short Connections
Connection, transfer data, close connection
A short connection is a socket connection that sends data and then disconnects immediately after receiving the data.
Like what:
HTTP1.0 default is a short connection, stateless, the browser and the server each HTTP operation, the establishment of a connection, but the end of the task to disconnect. 、
Http1.1 default is long connection
Stateless: The protocol has no memory capacity for transaction processing;
2.2 Long Connections
Connect, transfer data--keep connected, transfer data ... , close the connection.
After the socket connection is established, the connection remains consistent, regardless of whether it is used.
2.3 Half Pack
The receiving party did not receive a complete package, only accepted the part;
Reason: TCP to improve transmission efficiency, the allocation of a packet is large enough, resulting in the receiver can not be accepted at once.
Impact: Both long connections and short connections appear
2.4 Sticky Bag
The sender sends multiple packet data to the receiver when it is received and sticks it into a packet, looking from the receive buffer, followed by the header of the packet data immediately preceding the end of the packet of data.
Category: A package that is glued together is a complete packet, and the other case is that the package is stuck together with an incomplete package
There are many reasons for the sticky-bag phenomenon:
1) Sender Sticky packet: caused by the TCP protocol itself, TCP to improve transmission efficiency, the sender is often to collect enough data before sending a packet of data. If the data sent several times in a row is very small, usually TCP will be based on the optimization algorithm to synthesize the data packets sent out once, so that the receiver received the sticky packet data.
2) Receiving party adhesive package: The receiver user process does not receive data in a timely manner, resulting in sticky packet phenomenon. This is because the receiver first put the received data in the system receive buffer, the user process from the buffer to fetch data, if the next packet of data arrives before a packet of data has not been taken away by the user process, the next packet of data into the system receive buffer when the previous packet of data is received, The user process takes data from the system receive buffer based on the pre-set buffer size, so that it takes more than one packet of data at a time.
2.5 sub-package
Sub-Package (1): In the presence of sticky packets, our receiver will be sub-processing;
Subcontracting (2): A packet is divided into multiple receivers;
Cause: 1. (a) The transmission of IP shards; 2. The loss of some packets in the transmission process resulting in half of the package; 3. A packet may be divided into two transmissions, which is then taken as part of the data fetch (and possibly related to the size of the buffer received).
Impact: Sticky packets and subcontracting appear in long connections
2.6 How to solve the problem of half-pack, sticky bag
The appearance of sticky and half-packet phenomena is due to the concept that there is only a flow in TCP, without the concept of package.
UDP does not appear half-pack, sticky packet situation, because UDP is a complete packet, sent without merging, so when received there is no sticky packet.
Fixed length: Each time a fixed length of data is sent;
Special marking: With carriage return, newline as a special mark; When you get to the specified identity, the description package gets complete.
Byte length: Baotou + packet length + package of the Protocol form, when the server side to obtain the specified length of the package only to obtain the complete;
Reference article (with image): http://blog.csdn.net/pi9nc/article/details/17165171
2.7 When to consider the case of sticky bags
Short connection: Do not consider the case of sticky bag;
Send data without structure, such as file transfer, so that the sender just send, the receiver just receive storage, do not consider sticky packet;
Long connection: It is necessary to send different structure data within a period of time after connection;
Processing mode: The receiver creates a preprocessing thread, and the received packets are preprocessed, separating the packets of adhesion;
2.8 The difference between TCP and UDP
- Based on connection and no connection;
- Requirements for system resources (more TCP, less UDP);
- The structure of UDP program is relatively simple;
- Flow mode and datagram mode;
- TCP guarantees data correctness, UDP may drop packets, TCP guarantees data order, and UDP is not guaranteed.
- Network differentiation: The machine itself is good, but there is a problem between the communication;
- Network jitter: Latency in a network is the amount of delay in the transmission of information from sending to receiving, typically consisting of delay and processing delay, while jitter refers to the difference between the maximum delay and the minimum delay, such as a maximum delay of 20 milliseconds and a minimum delay of 5 milliseconds, then the network jitter is 15 milliseconds, which mainly identifies the stability of a network.
2.9 Other communication issues2.10 References
A package does not have a fixed length, Ethernet limit of 46-1500 bytes, 1500 is the MTU of the Ethernet, over this amount, TCP will set an offset for IP datagrams for the Shard transfer, now generally allows the application layer to set the 8k (NTFS system) buffer, 8k of data from the underlying shards, And the application seems to be just one send.
For UDP, do not be too large, usually 1024 to 10K. Note that you no matter how big the package, the IP layer and the link layer will send your packet to be fragmented, the general LAN is about 1500, WAN is only dozens of bytes. The packet after the Shard will reach the receiver by different routes, and for UDP, if one of the shards is lost, then the receiver's IP layer will discard the entire sending packet, which forms a packet loss.
TCP as a stream, the contract is not the whole package arrives, but a steady flow to, the receiver must pack. While UDP is a message or datagram, it must be the entire packet to the receiving party.
About the reception, the general contract has a package boundary, the first is the length of the package you want to let the receiver know, so there is a Baotou information, for TCP, the receiver first receive the packet header information, and then collect the data. It is also possible to collect the entire package at a time, and you can verify whether the results are received or not. This completes the package process as well. UDP, then you can only receive the entire packet. If the receive buffer you provide is too small, TCP will return the actual received length, the remaining can be received, and UDP is different, the remaining data is discarded and returned wsaemsgsize error. Note TCP, if you provide buffer like Blake Large, then you may receive multiple packages, you have to separate them, and when the buffer is too small, and at a time to receive the data inside the socket, then the socket receive event (OnReceive), may not be triggered again, When it is received using the event mode.
3 Socket Instance3.1 Socket Communication Model
3.2 Socket Architecture Complete Model
- Solution for half-pack, sticky Pack (codec)
- Server-Side Support multithreading
- Supports heartbeat detection
- Specifies the port instantiation of a Seversocket
- Call the Accept () method of serversocket to cause blocking while waiting for a connection
- Gets the stream at the bottom of the socket for read and write operations
- Encapsulate data into a stream
- Read and write to the socket
- Close an open stream
3.3 Service-side development
Establish the ServerSocket object and listen for the bound port
ServerSocket server=new ServerSocket (1000);
Establishing a receive socket, blocking the response
Socket client=server.accept ();
Get the input stream for receiving client information
InputStream In=server.getinputstream ();
Get the output stream for outputting client information
Packaging the input stream for ease of use
BufferedReader inread=new BufferedReader (New InputStreamReader (in));
Packaging the output stream for ease of use
PrintWriter outwriter=new PrintWriter (out);
while (true)
{
String str=inread.readline ();//Read in
Outwriter.println ("Output to Client");
Outwriter.flush ();//Output
if (Str.equals ("End"))
{
Break
}
}
Close socket
Client.close ();
Server.close ();
3.4 Client Development
- Instantiate a socket via IP address and port, requesting a connection to the server
- Get the stream on the socket for read and write
- Example of encapsulating flow into Bufferedreader/printwriter
- Read and write to the socket
- Close an open stream
Using a socket to establish a connection to the server
Socket client=new socket ("127.0.0.1", 1000);
InputStream In=client.getinputstream ();//Get input stream
OutputStream Out=client.getoutputstream ();//Get output stream
Wrapper input stream, output stream
BufferedReader inread=new BufferedReader (New InputStreamReader (in));
PrintWriter outwriter=new PrintWriter (out);
Get console input
BufferedReader inconsole=new BufferedReader (New InputStreamReader (in));
while (true)
{
String str=inconsole.readline ();//Read console input
Outwriter.println (str);//Output to Server
Outwriter.flush ();//Flush Buffer
if (Str.equals ("End"))
{
Break
}//exit
System.out.println (Inread.readline ())//Read service-side output
}
Client.close ();
DOS Running the client
Java-classpath Sockettest-0.0.1-snapshot.jar Cn.com.gome.sockettest.basic.ClientTest
3.5 Multi-threaded server
3.6 Heartbeat Detection
Method 1:socket.sendurgentdata (0);//send 1 bytes of emergency data, by default, the server side does not open the emergency processing, does not affect the normal communication
try{
Socket.sendurgentdata (0xFF);
}catch (Exception ex) {
Reconnect ();
}
This byte is automatically discarded as long as the So_oobinline property of the other socket is not open, and the So_oobinline property is turned off by default.
Method 2: Customize the heartbeat string
3.7 All kinds of data read and write
Transfer byte, character, object "ObjectInputStream ObjectOutputStream" (instance)
Reference:
Http://www.cnblogs.com/itfly8/p/5844803.html
Java Communication Series One: Java Socket technology summary