返回顶部
首页 > 资讯 > 操作系统 >网络协议分析-http/https/tcp/udp
  • 296
分享到

网络协议分析-http/https/tcp/udp

网络协议httphttpstcpupd 2023-08-30 10:08:14 296人浏览 安东尼
摘要

文章目录 TCP三次握手/TCP三次挥手TCP三次握手TCP四次挥手完整报文 实例代码HttpSampleClientHttpSampleServerHttpsSampleClientHttpsSampleServerTcpSa

tcp三次握手/TCP三次挥手

“三次握手”的目的是“为了防止已失效的连接请求报文段突然又传送到了服务端,因而产生 错误”。主要目的防止 server 端一直等待,浪费资源。换句话说,即是为了保证服务端能收 接受到客户端的信息并能做出正确的应答而进行前两次(第一次和第二次)握手,为了保证客 户端能够接收到服务端的信息并能做出正确的应答而进行后两次(第二次和第三次)握手。

“四次挥手”原因是因为 tcp 是全双工模式,接收到 FIN 时意味将没有数据再发来,但是还是 可以继续发送数据。
在这里插入图片描述

TCP三次握手

在这里插入图片描述

TCP四次挥手

在这里插入图片描述

完整报文

# 三次握手:SYN=1 Seq=0No.     Time           Source                Destination           Protocol Length Info     50 7.055660280    127.0.0.1             127.0.0.1             TCP      74     4415612345 [SYN] Seq=0 Win=65495 Len=0 MSS=65495 SACK_PERM TSval=2731439096 TSecr=0 WS=128Frame 50: 74 bytes on wire (592 bits), 74 bytes captured (592 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.312489778 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.312489778 seconds    [Time delta from previous captured frame: 0.004110298 seconds]    [Time delta from previous displayed frame: 0.000000000 seconds]    [Time since reference or first frame: 7.055660280 seconds]    Frame Number: 50    Frame Length: 74 bytes (592 bits)    Capture Length: 74 bytes (592 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP SYN/FIN]    [Coloring Rule String: tcp.flags & 0x02 || tcp.flags.fin == 1]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 0, Len: 0# 三次握手:SYN1 ACK=1 Seq=0No.     Time           Source                Destination           Protocol Length Info     51 7.055692471    127.0.0.1             127.0.0.1             TCP      74     1234544156 [SYN, ACK] Seq=0 Ack=1 Win=65483 Len=0 MSS=65495 SACK_PERM TSval=2731439096 TSecr=2731439096 WS=128Frame 51: 74 bytes on wire (592 bits), 74 bytes captured (592 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.312521969 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.312521969 seconds    [Time delta from previous captured frame: 0.000032191 seconds]    [Time delta from previous displayed frame: 0.000032191 seconds]    [Time since reference or first frame: 7.055692471 seconds]    Frame Number: 51    Frame Length: 74 bytes (592 bits)    Capture Length: 74 bytes (592 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP SYN/FIN]    [Coloring Rule String: tcp.flags & 0x02 || tcp.flags.fin == 1]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 0, Ack: 1, Len: 0# 三次握手:Ack=1 Seq=1No.     Time           Source                Destination           Protocol Length Info     52 7.055725041    127.0.0.1             127.0.0.1             TCP      66     4415612345 [ACK] Seq=1 Ack=1 Win=65536 Len=0 TSval=2731439096 TSecr=2731439096Frame 52: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.312554539 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.312554539 seconds    [Time delta from previous captured frame: 0.000032570 seconds]    [Time delta from previous displayed frame: 0.000032570 seconds]    [Time since reference or first frame: 7.055725041 seconds]    Frame Number: 52    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 1, Ack: 1, Len: 0# 客户端发送消息  Hello, server!.No.     Time           Source                Destination           Protocol Length Info     53 7.057834891    127.0.0.1             127.0.0.1             TCP      81     4415612345 [PSH, ACK] Seq=1 Ack=1 Win=65536 Len=15 TSval=2731439098 TSecr=2731439096Frame 53: 81 bytes on wire (648 bits), 81 bytes captured (648 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.314664389 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.314664389 seconds    [Time delta from previous captured frame: 0.002109850 seconds]    [Time delta from previous displayed frame: 0.002109850 seconds]    [Time since reference or first frame: 7.057834891 seconds]    Frame Number: 53    Frame Length: 81 bytes (648 bits)    Capture Length: 81 bytes (648 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp:data]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 1, Ack: 1, Len: 15Data (15 bytes)0000  48 65 6c 6c 6f 2c 20 73 65 72 76 65 72 21 0a      Hello, server!.# 服务端应答No.     Time           Source                Destination           Protocol Length Info     54 7.057859991    127.0.0.1             127.0.0.1             TCP      66     1234544156 [ACK] Seq=1 Ack=16 Win=65536 Len=0 TSval=2731439098 TSecr=2731439098Frame 54: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.314689489 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.314689489 seconds    [Time delta from previous captured frame: 0.000025100 seconds]    [Time delta from previous displayed frame: 0.000025100 seconds]    [Time since reference or first frame: 7.057859991 seconds]    Frame Number: 54    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 1, Ack: 16, Len: 0#  服务端发送消息 Server received: Hello, server!.No.     Time           Source                Destination           Protocol Length Info     55 7.059237157    127.0.0.1             127.0.0.1             TCP      98     1234544156 [PSH, ACK] Seq=1 Ack=16 Win=65536 Len=32 TSval=2731439100 TSecr=2731439098Frame 55: 98 bytes on wire (784 bits), 98 bytes captured (784 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.316066655 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.316066655 seconds    [Time delta from previous captured frame: 0.001377166 seconds]    [Time delta from previous displayed frame: 0.001377166 seconds]    [Time since reference or first frame: 7.059237157 seconds]    Frame Number: 55    Frame Length: 98 bytes (784 bits)    Capture Length: 98 bytes (784 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp:data]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 1, Ack: 16, Len: 32Data (32 bytes)0000  53 65 72 76 65 72 20 72 65 63 65 69 76 65 64 3a   Server received:0010  20 48 65 6c 6c 6f 2c 20 73 65 72 76 65 72 21 0a    Hello, server!.# 客户端还给了一个回应No.     Time           Source                Destination           Protocol Length Info     56 7.059273656    127.0.0.1             127.0.0.1             TCP      66     4415612345 [ACK] Seq=16 Ack=33 Win=65536 Len=0 TSval=2731439100 TSecr=2731439100Frame 56: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.316103154 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.316103154 seconds    [Time delta from previous captured frame: 0.000036499 seconds]    [Time delta from previous displayed frame: 0.000036499 seconds]    [Time since reference or first frame: 7.059273656 seconds]    Frame Number: 56    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 16, Ack: 33, Len: 0# 四次挥手 1 FIN=1 Ack=33 Seq=16No.     Time           Source                Destination           Protocol Length Info     57 7.062253079    127.0.0.1             127.0.0.1             TCP      66     4415612345 [FIN, ACK] Seq=16 Ack=33 Win=65536 Len=0 TSval=2731439103 TSecr=2731439100Frame 57: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.319082577 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.319082577 seconds    [Time delta from previous captured frame: 0.002979423 seconds]    [Time delta from previous displayed frame: 0.002979423 seconds]    [Time since reference or first frame: 7.062253079 seconds]    Frame Number: 57    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP SYN/FIN]    [Coloring Rule String: tcp.flags & 0x02 || tcp.flags.fin == 1]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 16, Ack: 33, Len: 0# 四次挥手 2 ACK=17 Seq=33No.     Time           Source                Destination           Protocol Length Info     58 7.110165407    127.0.0.1             127.0.0.1             TCP      66     1234544156 [ACK] Seq=33 Ack=17 Win=65536 Len=0 TSval=2731439151 TSecr=2731439103Frame 58: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.366994905 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.366994905 seconds    [Time delta from previous captured frame: 0.047912328 seconds]    [Time delta from previous displayed frame: 0.047912328 seconds]    [Time since reference or first frame: 7.110165407 seconds]    Frame Number: 58    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 33, Ack: 17, Len: 0# 四次挥手: 3 Fin=1 Ack=17 Seq=33No.     Time           Source                Destination           Protocol Length Info     61 7.390471256    127.0.0.1             127.0.0.1             TCP      66     1234544156 [FIN, ACK] Seq=33 Ack=17 Win=65536 Len=0 TSval=2731439431 TSecr=2731439103Frame 61: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.647300754 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.647300754 seconds    [Time delta from previous captured frame: 0.000234452 seconds]    [Time delta from previous displayed frame: 0.280305849 seconds]    [Time since reference or first frame: 7.390471256 seconds]    Frame Number: 61    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP SYN/FIN]    [Coloring Rule String: tcp.flags & 0x02 || tcp.flags.fin == 1]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 33, Ack: 17, Len: 0# 四次挥手 4 Ack=34 Seq=17No.     Time           Source                Destination           Protocol Length Info     62 7.390499061    127.0.0.1             127.0.0.1             TCP      66     4415612345 [ACK] Seq=17 Ack=34 Win=65536 Len=0 TSval=2731439431 TSecr=2731439431Frame 62: 66 bytes on wire (528 bits), 66 bytes captured (528 bits) on interface lo, id 0    Section number: 1    Interface id: 0 (lo)    Encapsulation type: Ethernet (1)    Arrival Time: Aug 29, 2023 08:26:32.647328559 EDT    [Time shift for this packet: 0.000000000 seconds]    Epoch Time: 1693311992.647328559 seconds    [Time delta from previous captured frame: 0.000027805 seconds]    [Time delta from previous displayed frame: 0.000027805 seconds]    [Time since reference or first frame: 7.390499061 seconds]    Frame Number: 62    Frame Length: 66 bytes (528 bits)    Capture Length: 66 bytes (528 bits)    [Frame is marked: False]    [Frame is ignored: False]    [Protocols in frame: eth:ethertype:ip:tcp]    [Coloring Rule Name: TCP]    [Coloring Rule String: tcp]Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 17, Ack: 34, Len: 0

实例代码

httpsampleClient

import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class HttpSampleClient {    public static void main(String[] args) {        try {            URL url = new URL("http://localhost:8080");            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setRequestMethod("GET");            int responseCode = connection.getResponseCode();            System.out.println("Response Code:" + responseCode);            // read content            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));            String line;            StringBuilder response = new StringBuilder();            while ((line = reader.readLine()) != null) {                response.append(line);            }            reader.close();            System.out.println("Response Body: " + response);            connection.disconnect();        } catch (Exception e) {            e.printStackTrace();        }    }}

HttpSampleServer

import com.sun.net.httpserver.HttpExchange;import com.sun.net.httpserver.HttpHandler;import com.sun.net.httpserver.HttpServer;import java.io.IOException;import java.io.OutputStream;import java.net.InetSocketAddress;public class HttpSampleServer {    public static void main(String[] args) {        try {            // create http server to bind 8080            HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 8080), 0);            // bind path            server.createContext("/", new MyHandler());            server.start();            System.out.println("Http Sever started on port 8080");        } catch (Exception e) {            e.printStackTrace();        }    }    static class MyHandler implements HttpHandler {        // 设置响应头c        @Override        public void handle(HttpExchange exchange) throws IOException {            String response = "Hello from the local HTTP server!";            exchange.sendResponseHeaders(200, response.getBytes().length);            // 获取输出流并写入响应内容            OutputStream os = exchange.getResponseBody();            os.write(response.getBytes());            os.close();        }    }}

HttpsSampleClient

import javax.net.ssl.*;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;public class HttpsSampleClient {    public static void main(String[] args) {        try {            // Create and configure the SSL context            SSLContext sslContext = SSLContext.getInstance("TLS");            sslContext.init(null, new TrustManager[]{new X509TrustManager() {                public java.security.cert.X509Certificate[] getAcceptedIssuers() {                    return null;                }                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {                }                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {                }            }}, new java.security.SecureRandom());            // Create an SSL socket            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();            SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket("localhost", 12345);            // Get input and output streams for secure communication            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); // autoFlush            // Send a message to the server            String message = "Hello, server!";            writer.println(message);            // Read response from the server            String serverResponse = reader.readLine();            System.out.println("Server response: " + serverResponse);            // Close resources            reader.close();            writer.close();            socket.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

HttpsSampleServer

import javax.net.ssl.*;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.security.KeyStore;public class HttpsSampleServer {    public static void main(String[] args) {        String keystorePath = "./keystore.jks";        char[] keystorePassWord = "flannery".toCharArray();        try {            // Load the keystore containing the server's SSL certificate            KeyStore keystore = KeyStore.getInstance("JKS");            keystore.load(new FileInputStream(keystorePath), keystorePassword);            // Create and configure the SSL context            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");            kmf.init(keystore, keystorePassword);            SSLContext sslContext = SSLContext.getInstance("TLS");            sslContext.init(kmf.geTKEyManagers(), null, null);            // Create an SSL server socket            SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();            SSLServerSocket serverSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(12345);            System.out.println("Server listening on port 12345...");            // Wait for a client to connect            SSLSocket clientSocket = (SSLSocket) serverSocket.accept();            System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());            // Get input and output streams for secure communication            BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));            PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true); // autoFlush            // Read data from client and respond            String clientMessage;            while ((clientMessage = reader.readLine()) != null) {                System.out.println("Received from client: " + clientMessage);                writer.println("Server received: " + clientMessage);            }            // Close resources            reader.close();            writer.close();            clientSocket.close();            serverSocket.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

TcpSampleClient

import java.io.*;import java.net.Socket;public class TcpSampleClient {    public static void main(String[] args) {        try {            // Create a socket to connect to the server            Socket socket = new Socket("localhost", 12345);            // Get input and output streams for the socket            InputStream inputStream = socket.getInputStream();            OutputStream outputStream = socket.getOutputStream();            // Create readers and writers for easier communication            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));            PrintWriter writer = new PrintWriter(outputStream, true); // autoFlush            // Send a message to the server            String message = "Hello, server!";            writer.println(message);            // Read response from the server            String serverResponse = reader.readLine();            System.out.println("Server response: " + serverResponse);            // Close resources            reader.close();            writer.close();            socket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

TcpSampleServer

import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class TcpSampleServer {    public static void main(String[] args) {        extracted();    }    private static void extracted() {        try {            // Create a server socket            ServerSocket serverSocket = new ServerSocket(12345);            System.out.println("Server listening on port 12345...");            // Wait for a client to connect            Socket clientSocket = serverSocket.accept();            System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());            // Get input and output streams for the socket            InputStream inputStream = clientSocket.getInputStream();            OutputStream outputStream = clientSocket.getOutputStream();            // Create readers and writers for easier communication            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));            PrintWriter writer = new PrintWriter(outputStream, true); // autoFlush            // Read data from client and respond            String clientMessage;            while ((clientMessage = reader.readLine()) != null) {                System.out.println("Received from client: " + clientMessage);                writer.println("Server received: " + clientMessage);            }            // Close resources            //reader.close();            //writer.close();            //clientSocket.close();            //serverSocket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

UdpSampleClient

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UdpSampleClient {public static void main(String[] args) {        try {            DatagramSocket clientSocket = new DatagramSocket();            String message = "Hello, server!";            byte[] sendData = message.getBytes();            InetAddress serverAddress = InetAddress.getByName("localhost");            int serverPort = 12345;            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, serverPort);            clientSocket.send(sendPacket);            byte[] receiveData = new byte[1024];            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);            clientSocket.receive(receivePacket);            String serverResponse = new String(receivePacket.getData(), 0, receivePacket.getLength());            System.out.println("Server response: " + serverResponse);            clientSocket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

UdpSampleSever

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UdpSampleSever {    public static void main(String[] args) {        try {            DatagramSocket serverSocket = new DatagramSocket(12345);            System.out.println("Server listening on port 12345...");            byte[] receiveData = new byte[1024];            while (true) {                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);                serverSocket.receive(receivePacket);                String clientMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());                System.out.println("Received from client: " + clientMessage);                InetAddress clientAddress = receivePacket.getAddress();                int clientPort = receivePacket.getPort();                String serverResponse = "Server received: " + clientMessage;                byte[] sendData = serverResponse.getBytes();                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);                serverSocket.send(sendPacket);            }        } catch (IOException e) {            e.printStackTrace();        }    }}

资料

超详细的wireshark抓包使用教程
jks证书文件的生成步骤
如何使用wireshark抓取HTTPS数据包?
wireshark抓包分析HTTPS
Burp Suite的基本介绍及使用
渗透测试工具Burp Suite详解
vmware ctrl键粘滞的问题
wireshark过滤规则
Burp Suite HTTPS详解

来源地址:https://blog.csdn.net/AdrianAndroid/article/details/132568972

--结束END--

本文标题: 网络协议分析-http/https/tcp/udp

本文链接: https://lsjlt.com/news/382530.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • 网络协议分析-http/https/tcp/udp
    文章目录 TCP三次握手/TCP三次挥手TCP三次握手TCP四次挥手完整报文 实例代码HttpSampleClientHttpSampleServerHttpsSampleClientHttpsSampleServerTcpSa...
    99+
    2023-08-30
    网络协议 http https tcp upd
  • 网络编程协议(TCP和UDP协议,粘包问
    网络编程协议 1.osi七层模型 应用层  表示层  会话层  传输层  网络层  数据链路层  物理层 2.套接字 socket  有两类,一种基于文件类型,一种基于网络类型 3.Tcp和udp协议 Tcp协议:面向连接,数据可靠,传输...
    99+
    2023-01-30
    协议 网络编程 TCP
  • http和https网络协议是什么
    这篇文章主要介绍http和https网络协议是什么,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!http和https网络协议究竟是什么?在选择代理ip之前,我们需要弄清楚它们的意义吗?是因特网上使用最广泛的一种网络协...
    99+
    2023-06-15
  • 详情解析TCP与UDP传输协议
    目录一、什么是 socket ?二、Socket 编程的重要概念① IP 地址② TCP/IP 端口③ 协议三、socket 编程的 API 接口① Linux 下的 socket ...
    99+
    2024-04-02
  • 【Python爬虫与数据分析】UDP/TCP通信协议
    目录 一、网络编程基础 二、UDP协议 三、TCP协议 一、网络编程基础 数据编码与解码 str -> bytes:encode编码,发送信息的时候用encode编码bytes -> str:decode解码,打印接收的信息用decod...
    99+
    2023-09-06
    python udp tcp/ip 网络通信
  • 【网络】HTTP协议详解
    😀大家好,我是白晨,一个不是很能熬夜😫,但是也想日更的人✈。如果喜欢这篇文章,点个赞👍,关注一下👀白晨吧!你的支持就是我最大的动力!💪💪...
    99+
    2023-08-23
    网络 http 网络协议 服务器 c++
  • python 网络通信协议/TCP,UD
    一.osi七层协议 互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层 协议具体内容   各层的功能简述: 【1】物理层:主要定义物理设备标准,如网线的接口类型、光纤的接口类型、各种传输介质的传输速率...
    99+
    2023-01-30
    通信协议 网络 python
  • HTTP协议实例分析
    这篇文章主要讲解了“HTTP协议实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“HTTP协议实例分析”吧! 在web2.0时代,互联网从传统的C/S...
    99+
    2024-04-02
  • modbus tcp协议介绍及分析
    一、modbus tcp协议介绍 Modbus TCP协议是基于TCP/IP网络的Modbus协议的一种变体。它将Modbus协议封装在TCP/IP协议栈中,使用以太网作为物理层,通过TCP连接来实现设备之间的通信。 Modbus TC...
    99+
    2023-09-20
    tcp/ip 网络 服务器 网络协议 运维
  • Java网络编程之基于TCP协议
    目录一、单向通信二、双向通信三、对象流传送四、加入完整的处理异常方式五、多线程接收用户请求一、单向通信 功能:客户端发送一句话到服务器: 客户端: public class ...
    99+
    2024-04-02
  • 详解计算机网络TCP/IP协议
    这篇文章主要介绍“详解计算机网络TCP/IP协议”,在日常操作中,相信很多人在详解计算机网络TCP/IP协议问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”详解计算机网络TCP...
    99+
    2024-04-02
  • 计算机网络之十三:HTTPS协议
    一:对称加密在对称加密算法中,加密和解密使用的密钥是相同的。二:非对称加密在非对称加密算法中,加密使用的密钥和解密使用的密钥是不相同的。一是作为公开的公钥,一是作为谁都不能给的私钥。三:数字证书1.数字证书简介数字证书是互联网通信中标志通信...
    99+
    2023-06-04
  • Java实现基于UDP协议的网络通信UDP编程
    目录UDP编程通信基本介绍基本流程UDP编程通信 基本介绍 类DatagramSocket和DatagramPacket【数据包/数据报】实现了基于UDP协议网络程序。UDP数据报通...
    99+
    2023-05-17
    Java UDP编程 Java UDP网络编程
  • 如何分析Linux TCP/IP协议栈
    这期内容当中小编将会给大家带来有关如何分析Linux TCP/IP协议栈,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。TCP/IP是指能够在多个不同网络间实现信息传输的协议簇。TCP/IP协议不仅仅指的是...
    99+
    2023-06-28
  • 怎么解析C#网络编程中的HTTP协议
    这篇文章将为大家详细讲解有关怎么解析C#网络编程中的HTTP协议,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。我们在用Asp.net技术开发Web应用程序后,当用户在浏览器输入一个网址时就是...
    99+
    2023-06-17
  • 【计算机网络】传输层协议——TCP(下)
    文章目录 1. 拥塞控制慢启动滑动窗口最终大小为什么采用指数增加的方案,来设计对应的拥塞窗口的增加速度? 2. 延迟应答3. 粘包问题4. TCP异常情况(面试题)进程终止机器重启网线断开...
    99+
    2023-09-20
    计算机网络 tcp/ip
  • 【计算机网络】传输层协议——TCP(中)
    文章目录 1. 三次握手三次握手的本质是建立链接,什么是链接?整体过程三次握手过程中报文丢失问题为什么2次握手不可以?为什么要三次握手? 2. 四次挥手整体过程为什么要等待2MSL 3...
    99+
    2023-09-20
    计算机网络 tcp/ip 网络
  • 网络传输模型/TCP协议/IP地址
    基本模型 层次划分 需要说明的是在网络传输层TCP可靠而UDP不可靠 传输层说明 说明一: 作为Python开发,咱们都是在应用层的HTTP协议之上进行开发的。 说明二: 网络编程,主要是了解我们Python能编写的最低的层次, 即...
    99+
    2023-01-30
    网络传输 模型 协议
  • Java的http协议实例分析
    本篇内容主要讲解“Java的http协议实例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java的http协议实例分析”吧!http协议的组成 http...
    99+
    2024-04-02
  • Http协议Content-Length实例分析
    本篇内容主要讲解“Http协议Content-Length实例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Http协议Content-Length实例分析...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作