1. A brief introduction

Let’s start with two concepts:

Short connection:

  • Connect -> Transfer Data -> Close the connection
  • HTTP is stateless; each time the browser and server perform an HTTP operation, a connection is established, but the connection is broken at the end of the task.
  • Short connection means that the Socket is disconnected immediately after sending and receiving data.

Long connection:

  • Connect -> Transfer data -> Keep the connection -> Transfer data ->… -> Close the connection.
  • Long connection indicates that the connection is maintained regardless of whether the Socket connection is used or not after it is established. However, the connection is insecure.

Half packet: Indicates that the receiving end does not receive a complete packet, but only receives part of the packet. This is mainly because TCP allocates a packet to a large enough size to improve transmission efficiency. As a result, the receiving end cannot receive all the packets at one time. (occurs in both long and short connections).

Sticky packet: several packets sent by the sender are stuck into a packet when received by the receiver. From the view of the receiving buffer, the first packet of data is followed by the last packet of data. There are many reasons for sticky packet phenomenon, which may be caused by sender or receiver.

Subcontracting: refers to the subcontracting process by the receiving party in the case of sticky packages.

Note: There are two types of sticky packets. One is that the stuck packets are complete packets, and the other is that the stuck packets have incomplete packets.

The phenomenon of sticky packets and half packets occurs because TCP only has the concept of stream, not packet.

Example 2.

Recently use Socket (from machine) to read device data (host), according to the protocol to do a simple example code (implementation of subcontracting) as follows:

package org.tempuri; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class Test { private static Socket socket = null; private static BufferedInputStream read; private static BufferedOutputStream write; IEC104 protocol / * * * * * said the second data length/private static int [] activation = new int [] {x07 x68 0, 0 x04, 0, 0 x00 to 0 x00 to 0 x00}; Private static int[] totalCall = new X0e x68 int [] {0, 0, 0 x00 to 0 x00 to 0 x00 to 0 x00 to 0 x64, 0 x01, 0 x06, 0 x00 to 0 x01, 0 x00 to 0 x00 to 0 x00 to 0 x00 to 0 x14}; / / total call private static int [] sendS = new int [] {0 x68, 0 x04, 0 x01, 0 x00 to 0 x00 to 0 x00}; Public static void main(String[] args) throws Exception {while(true){try {socket = new socket ("127.0.0.1", 2404); read = new BufferedInputStream(socket.getInputStream()); write = new BufferedOutputStream(socket.getOutputStream()); sendData(activation); sendData(totalCall); read(); } catch (Exception e) {system.out.println (" connection failed......") ); Public static void read(){List<Byte> BTS = new ArrayList<Byte>(); byte[] btsb = new byte[512]; int bLen; int dataLen=0; long time = System.currentTimeMillis(); while(true){ try { bLen = read.read(btsb); if(bLen ! = -1){ time = System.currentTimeMillis(); for(int i=0; i<bLen; i++){ byte b = btsb[i]; if(bts.size() == 0){ if(b ! = 0x68)// Protocol header is not 0x68 skip continue; else bts.add(b); }else{ bts.add(b); if(bts.size() == 2){ dataLen = (0XFF & b) + 2; Else if(bts.size() >= dataLen){byte[] temp = new byte[bts.size()]; for(int j=0; j<bts.size(); j++) temp[j]=bts.get(j); Println (" Receive: "+getStrByByteArr(temp)); If (temp[2] &1) == 1)//S frame sendData(send); bts.clear(); }}}} if(system.currentTimemillis () -time > 8000)// send sendData(send) for 8 seconds; } catch (Exception e) { break; Public static synchronized void exitSocket(){try {if(socket! = null) socket.close(); if(read ! = null) read.close(); if(write ! = null) write.close(); } catch (IOException e1) {system.out.println (" closing error "); }finally{system.out. println(new SimpleDateFormat(" YYYY-MM-DD HH: MM :ss").format(new Date())+"; Public static synchronized void sendData(int[] MSG) throws Exception{system.out.println (" -----"+getStrByByteArr(iCB(msg))); write.write(iCB(msg)); write.flush(); } public static byte[] iCB(int[] arr){ if(arr==null) return null; byte[] narr = new byte[arr.length]; for(int i=0; i<arr.length; i++){ narr[i] = (byte)arr[i]; } return narr; } public static String getStrByByteArr(byte[] bts){ StringBuffer sb = new StringBuffer(); for(int i=0; i<bts.length; i++){ String str = Integer.toHexString(bts[i]&0xFF); if(str.length()==1) str="0"+str; sb.append(" ").append(str); } return sb.toString().toUpperCase(); }}Copy the code