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

文章目录

  • TCP三次握手/TCP三次挥手
    • TCP三次握手
    • TCP四次挥手
    • 完整报文
  • 实例代码
    • HttpSampleClient
    • HttpSampleServer
    • HttpsSampleClient
    • HttpsSampleServer
    • TcpSampleClient
    • TcpSampleServer
    • UdpSampleClient
    • UdpSampleSever
  • 资料

TCP三次握手/TCP三次挥手

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

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

TCP三次握手

在这里插入图片描述

TCP四次挥手

在这里插入图片描述

完整报文

# 三次握手:SYN=1 Seq=0
No.     Time           Source                Destination           Protocol Length Info50 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 0Section number: 1Interface 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: 50Frame 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 Info51 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 0Section number: 1Interface 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: 51Frame 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 Info52 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 0Section number: 1Interface 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: 52Frame 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 Info53 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 0Section number: 1Interface 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: 53Frame 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 Info54 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 0Section number: 1Interface 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: 54Frame 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 Info55 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 0Section number: 1Interface 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: 55Frame 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 Info56 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 0Section number: 1Interface 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: 56Frame 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 Info57 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 0Section number: 1Interface 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: 57Frame 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 Info58 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 0Section number: 1Interface 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: 58Frame 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 Info61 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 0Section number: 1Interface 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: 61Frame 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 Info62 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 0Section number: 1Interface 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: 62Frame 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 contentBufferedReader 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 8080HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 8080), 0);// bind pathserver.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@Overridepublic 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 contextSSLContext 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 socketSSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket("localhost", 12345);// Get input and output streams for secure communicationBufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); // autoFlush// Send a message to the serverString message = "Hello, server!";writer.println(message);// Read response from the serverString serverResponse = reader.readLine();System.out.println("Server response: " + serverResponse);// Close resourcesreader.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 certificateKeyStore keystore = KeyStore.getInstance("JKS");keystore.load(new FileInputStream(keystorePath), keystorePassword);// Create and configure the SSL contextKeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");kmf.init(keystore, keystorePassword);SSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(kmf.getKeyManagers(), null, null);// Create an SSL server socketSSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();SSLServerSocket serverSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(12345);System.out.println("Server listening on port 12345...");// Wait for a client to connectSSLSocket clientSocket = (SSLSocket) serverSocket.accept();System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());// Get input and output streams for secure communicationBufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true); // autoFlush// Read data from client and respondString clientMessage;while ((clientMessage = reader.readLine()) != null) {System.out.println("Received from client: " + clientMessage);writer.println("Server received: " + clientMessage);}// Close resourcesreader.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 serverSocket socket = new Socket("localhost", 12345);// Get input and output streams for the socketInputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();// Create readers and writers for easier communicationBufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));PrintWriter writer = new PrintWriter(outputStream, true); // autoFlush// Send a message to the serverString message = "Hello, server!";writer.println(message);// Read response from the serverString serverResponse = reader.readLine();System.out.println("Server response: " + serverResponse);// Close resourcesreader.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 socketServerSocket serverSocket = new ServerSocket(12345);System.out.println("Server listening on port 12345...");// Wait for a client to connectSocket clientSocket = serverSocket.accept();System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());// Get input and output streams for the socketInputStream inputStream = clientSocket.getInputStream();OutputStream outputStream = clientSocket.getOutputStream();// Create readers and writers for easier communicationBufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));PrintWriter writer = new PrintWriter(outputStream, true); // autoFlush// Read data from client and respondString 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详解

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/117736.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

YOLO目标检测——视觉显著性检测MSRA1000数据集下载分享

MSRA1000数据集是一个常用的视觉显著性检测数据集,它包含了1000张图像和对应的显著性标注。在以下几个应用场景中,MSRA1000数据集可以发挥重要作用:图像编辑和后期处理、图像检索和分类、视觉注意力模型、自动驾驶和智能交通等等 数据集点击下…

鸿鹄企业工程项目管理系统 Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统源代码

鸿鹄工程项目管理系统 Spring CloudSpring BootMybatisVueElementUI前后端分离构建工程项目管理系统 1. 项目背景 一、随着公司的快速发展,企业人员和经营规模不断壮大。为了提高工程管理效率、减轻劳动强度、提高信息处理速度和准确性,公司对内部工程管…

内存管理方式

内存管理 一、C/C内存分布1、内存空间的介绍2、示例题目3、示例题目图解 二、C语言动态内存管理方式1、代码2、介绍 三、C内存管理方式1、概念2、代码3、代码所代表的意义 四、new和delete操作自定义类型1、代码2、运行结果3、特点 五、operator new与operator delete函数1、概…

IP网络广播系统有哪些优点

IP网络广播系统有哪些优点 IP网络广播系统有哪些优点? IP网络广播系统是基于 TCP/IP 协议的公共广播系统,采用 IP 局域网或 广域网作为数据传输平台,扩展了公共广播系统的应用范围。随着局域网络和 网络的发展 , 使网络广播的普及变为可能 …

【一等奖方案】大规模金融图数据中异常风险行为模式挖掘赛题「NUFE」解题思路

第十届CCF大数据与计算智能大赛(2022 CCF BDCI)已圆满结束,大赛官方竞赛平台DataFountain(简称DF平台)正在陆续释出各赛题获奖队伍的方案思路,欢迎广大数据科学家交流讨论。 本方案为【大规模金融图数据中…

Yao框架浏览器打开后Cannot read properties of undefined (reading ‘search‘)

Yao创建项目后浏览器打开报 undefined 错误,是因为 Yao 框架语言包的问题,只支持浏览器的【中文(简体)】【英语(美国)】,如果浏览器排第一的语言不是这两个就会出现这个错误。临时解决方案是在浏…

内网穿透神器-frp的概念,搭建和使用,方便访问内网服务

FRP概念 FRP是什么(借助官网的描述)? frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议,且支持 P2P 通信。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。 为什么是用FR…

计算机毕设 基于生成对抗网络的照片上色动态算法设计与实现 - 深度学习 opencv python

文章目录 1 前言1 课题背景2 GAN(生成对抗网络)2.1 简介2.2 基本原理 3 DeOldify 框架4 First Order Motion Model5 最后 1 前言 🔥 这两年开始毕业设计和毕业答辩的要求和难度不断提升,传统的毕设题目缺少创新和亮点,往往达不到毕业答辩的要…

CXL.cache H2D/D2H 请求响应对应关系

🔥点击查看精选 CXL 系列文章🔥 🔥点击进入【芯片设计验证】社区,查看更多精彩内容🔥 📢 声明: 🥭 作者主页:【MangoPapa的CSDN主页】。⚠️ 本文首发于CSDN&#xff0c…

CUDA小白 - NPP(3) 图像处理 Color and Sampling Conversion

cuda小白 原始API链接 NPP GPU架构近些年也有不少的变化,具体的可以参考别的博主的介绍,都比较详细。还有一些cuda中的专有名词的含义,可以参考《详解CUDA的Context、Stream、Warp、SM、SP、Kernel、Block、Grid》 常见的NppStatus&#xf…

springboot:时间格式化的5种方法(解决后端传给前端的时间格式转换问题)推荐使用第4和第5种!

本文转载自:springboot:时间格式化的5种方法(解决后端传给前端的时间显示不一致)_为什么前端格式化日期了后端还要格式化_洛泞的博客-CSDN博客 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项目&#xff…

信息技术01--初/高中--选择真题汇总(197道题)

文章目录 1 真题 01-102 真题 11-203 真题 21-304 真题 31-405 真题 41-506 真题 51-607 真题 61-708 真题 71-809 真题 81-9010 真题 91-10011 真题 101-11012 真题 111-12013 真题 121-13014 真题 131-14015 真题 141-15016 真题 151-16017 真题 161-17018 真题 171-18019 真…

Java运行时jar时终端输出的中文日志是乱码

运行Jar时在控制台输出的中文日志全是乱码,这是因为cmd/bash默认的编码是GBK,只要把cmd的编码改成UTF-8即可 两种方式修改:临时修改和注册表永久修改 临时修改 只对当前的cmd页面有效,关闭后重新打开都会恢复成GBK, 打开cmd&am…

C盘清理 “ProgramData\Microsoft\Search“ 文件夹过大

修改索引存放位置 进入控制面板->查找方式改成大图标, 选择索引选项 进入高级 填写新的索引位置 删除C盘索引信息 删除C:\ProgramData\Microsoft\Search\Data\Applications 下面的文件夹 如果报索引正在使用,参照第一步替换索引位置。关闭索引

泊松回归和地理加权泊松回归

01 泊松回归 泊松回归(Poisson Regression)是一种广义线性模型,用于建立离散型响应变量(计数数据)与一个或多个预测变量之间的关系。它以法国数学家西蒙丹尼泊松(Simon Denis Poisson)的名字命名,适用于计算“事件发生次数”的概率,比如交通事故发生次数、产品缺陷数…

微服务-gateway鉴权

文章目录 一、前言二、gateway鉴权1、依赖配置2、编写代码3、GlobalFilter详解3.1、GlobalFilter简介3.2、GlobalFilter自定义执行顺序3.2.1、实现Order接口实现自定义执行顺序 一、前言 网关是介于客户端和服务器端之间的中间层,所有的外部请求都会先经过 网关这一…

Flutter:getX的学习

前言 学习教程:Getx教程_FlutterGetx系列实战教程 简介 getX是第三方的状态管理插件,不仅具有状态管理的功能,还具有路由管理、主题管理、国际化多语言管理、网络请求、数据验证等功能。相比其他状态管理组件,getX简单、功能强大…

“智越”界限,SSOT上海国际智慧办公展览会来啦

随着人工智能、大数据、云计算等技术的快速发展,目前物联网应用已经从概念踏进规模部署的阶段,场景化和规模化已成常态。传统办公室作为企业行政运营的核心场景,也开始受到“科技办公“移动办公”“共享办公”等非传“智慧办公”概念的影响不…

vscode远程调试php

使用vscode远程调试php的方法 1.安装remote ssh插件 2.连接服务器 可以点击左下角的绿色按钮,或者ctrlshiftp打开命令框输入remote ssh应该也有。 3.在服务器端vscode安装php debug插件 4.安装xdebug xdebug是用来调试php的软件,原本和vscode没什么关…

04、添加 com.fasterxml.jackson.dataformat -- jackson-dataformat-xml 依赖报错

Correct the classpath of your application so that it contains a single, compatible version of com.fasterxml.jackson.dataformat.xml.XmlMapper 解决: 改用其他版本,我没写版本号,springboot自己默认的是 2.11.4 版本 成功启动项目…