參考資料:
https://huoding.com/2013/11/21/299
https://hpbn.co/building-blocks-of-tcp/#three-way-handshakejava
示例代碼:
https://github.com/gordonklg/study,socket modulegit
免費抓包工具,誰用誰知道。
根據端口過濾 frame 的方法:tcp.port==8888
默認安裝的 Winpcap 不能對 localhost 抓包,建議安裝 Npcap。github
上圖描述了 TCP 創建鏈接過程當中三次握手的過程。socket
寫一段測試代碼創建鏈接,以便 WireShark 抓包分析:
gordon.study.socket.basic.wireshark.ThreeWayHandshake.javatcp
1 public class ThreeWayHandshake { 2 3 @SuppressWarnings("resource") 4 public static void main(String[] args) throws Exception { 5 new Thread(new Runnable() { 6 @Override 7 public void run() { 8 try { 9 ServerSocket serverSocket = new ServerSocket(8888); 10 Socket socket = serverSocket.accept(); 11 while(true) { 12 System.out.println(socket.getInputStream().read()); 13 } 14 } catch (Exception e) { 15 } 16 } 17 }).start(); 18 19 Socket socket = new Socket(); 20 socket.connect(new InetSocketAddress(8888)); 21 } 22 }
上圖爲 WireShark 抓取的 TCP 三次握手過程當中的報文,三條記錄分別對應三次握手過程。當前選中的 Frame 13 是客戶端的 SYN 請求,其前4個 Bytes 是鏈路層相關的信息(不知何意),中間20個 Bytes 是 IPv4 數據報首部,最後32個 Bytes 是 TCP 報文段。ide
照着上面 TCP 報文段格式圖表,咱們能夠分析出:工具