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

这篇具有很好参考价值的文章主要介绍了网络协议分析-http/https/tcp/udp。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

TCP三次握手/TCP三次挥手

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

“四次挥手”原因是因为 tcp 是全双工模式,接收到 FIN 时意味将没有数据再发来,但是还是 可以继续发送数据。
网络协议分析-http/https/tcp/udp,网络协议,http,https,tcp,upd

TCP三次握手

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

TCP四次挥手

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

完整报文

# 三次握手:SYN=1 Seq=0
No.     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=128

Frame 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.1
Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 0, Len: 0

# 三次握手:SYN1 ACK=1 Seq=0
No.     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=128

Frame 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.1
Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 0, Ack: 1, Len: 0

# 三次握手:Ack=1 Seq=1
No.     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=2731439096

Frame 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.1
Transmission 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=2731439096

Frame 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.1
Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 1, Ack: 1, Len: 15
Data (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=2731439098

Frame 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.1
Transmission 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=2731439098

Frame 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.1
Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 1, Ack: 16, Len: 32
Data (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=2731439100

Frame 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.1
Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 16, Ack: 33, Len: 0

# 四次挥手 1 FIN=1 Ack=33 Seq=16
No.     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=2731439100

Frame 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.1
Transmission Control Protocol, Src Port: 44156, Dst Port: 12345, Seq: 16, Ack: 33, Len: 0

# 四次挥手 2 ACK=17 Seq=33
No.     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=2731439103

Frame 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.1
Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 33, Ack: 17, Len: 0

# 四次挥手: 3 Fin=1 Ack=17 Seq=33
No.     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=2731439103

Frame 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.1
Transmission Control Protocol, Src Port: 12345, Dst Port: 44156, Seq: 33, Ack: 17, Len: 0

# 四次挥手 4 Ack=34 Seq=17
No.     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=2731439431

Frame 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.1
Transmission 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://www.toymoban.com/news/detail-681126.html

到了这里,关于网络协议分析-http/https/tcp/udp的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【计算机网络实验】实验四 TCP/UDP协议分析

    目录 一、TCP连接建立过程分析 二、TCP关闭连接过程分析 三、6号报文分析 四、A方TCP报文序列号分析 五、计算 六、UDP协议分析 图 1 第一次握手 第一次握手:客户端将标志位 SYN 置为 1 ,随机产生一个值 SEQ = X = 0 ,并将该数据包发送给服务器,等待服务器确认; 图 2 第二次

    2024年02月03日
    浏览(51)
  • 前端面试:【网络协议与性能优化】HTTP/HTTPS、TCP/IP和WebSocket

    嗨,亲爱的Web开发者!在构建现代Web应用时,了解网络协议是优化性能和确保安全性的关键。本文将深入探讨HTTP/HTTPS、TCP/IP和WebSocket这三个网络协议,帮助你理解它们的作用以及如何优化Web应用的性能。 1. HTTP/HTTPS协议: HTTP(超文本传输协议): HTTP是用于在Web上传输数据的

    2024年02月11日
    浏览(48)
  • 网络协议分析(一)(C语言实现---ethernet、arp、ip、icmp、udp、tcp)

    以太网协议是一种局域网通信协议,它通过物理层和数据链路层的协同工作,使用媒体访问控制地址和载波监听/冲突检测协议来实现计算机之间的稳定数据传输。在数据传输过程中,以太网会将数据封装成数据帧,并根据目标MAC地址来识别需要接收数据的计算机。通过这种方

    2024年02月06日
    浏览(50)
  • 一文彻底搞懂TCP/IP、UDP、FTP、HTTP、HTTPS的基本原理

    基本原理:TCP/IP是一组用于实现网络通信的协议族,它是互联网的基础协议。TCP/IP协议族由两个主要协议组成:TCP(Transmission Control Protocol,传输控制协议)和IP(Internet Protocol,网际协议)。 TCP(Transmission Control Protocol):TCP是一种面向连接的、可靠的、基于字节流的传输层

    2024年03月08日
    浏览(36)
  • UDP/TCP协议报头详细分析

    数据段=报头+数据 netstat是一个用来查看网络状态的重要工具. 语法:netstat [选项] 功能:查看网络状态 常用选项: n 拒绝显示别名,能显示数字的全部转化成数字 l 仅列出有在 Listen (监听) 的服務状态 p 显示建立相关链接的程序名 t (tcp)仅显示tcp相关选项 u (udp)仅显示udp相关选

    2024年02月10日
    浏览(39)
  • TCP/IP,UDP,DNS,NAT,DHCP,HTTP协议

    1.TCP/IP协议 TCP/IP协议是Internet最基本的协议、Internet国际互联网络的基础,由网络层的IP协议和传输层的TCP协议组成。通俗而言:TCP负责发现传输的问题,一有问题就发出信号,要求重新传输,直到所有数据安全正确地传输到目的地。而IP是给因特网的每一台联网设备规定一个

    2024年01月17日
    浏览(48)
  • TCP、IP、TCP/IP、HTTP和HTTPS协议简介

    TCP、IP、TCP/IP、HTTP和HTTPS都是计算机网络通信中常见的协议。 TCP TCP(Transmission Control Protocol)是传输层协议,在网络通信中提供可靠的、面向连接的数据传输服务。TCP协议确保数据的可靠性、完整性和有序性,并且可以自动进行重传和错误校验,为应用层提供了可靠的数据传输

    2024年02月04日
    浏览(45)
  • 网络:TCP、UDP协议

    目录 netstat pidof xargs UDP协议 TCP协议 setsockopt函数 确认应答机制 超时重传机制 流量控制 滑动窗口 拥塞控制 延迟应答 捎带应答 面向字节流 粘包问题 在TCP/IP协议中, 用 \\\"源IP\\\", \\\"源端口号\\\", \\\"目的IP\\\", \\\"目的端口号\\\", \\\"协议号\\\" 这样一个五元组来标识一个通信 0- 1023:知名端口号, HTTP

    2024年04月17日
    浏览(41)
  • 【计算机网络】UDP协议与TCP协议

    端口号(Port)标识了一个主机上进行通信的不同的应用程序; 在TCP/IP协议中, 用 “源IP”, “源端口号”, “目的IP”, “目的端口号”, “协议号” 这样一个五元组来标识一个通信(可以通过netstat -n查看); 服务器的同一个服务会使用多进程或者多线程的方式,对于不同的客户端请

    2024年01月25日
    浏览(56)
  • 【传输层】网络基础 -- UDP协议 | TCP协议

    端口号(Port)标识了一个主机上进行通信的不同的应用程序 在TCP/IP协议中,用 “源IP”, “源端口号”, “目的IP”, “目的端口号”, “协议号” 这样一个五元组来标识一个通信(可以通过 netstat -n 查看) 0 - 1023:知名端口号,HTTP,FTP,SSH等这些广为使用的应用层协议,他

    2024年02月09日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包