UDP
UDPServer
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args) throws IOException {
System.out.println("UdpServer启动");
// 创建upd套接字
DatagramSocket socket = new DatagramSocket(8080);
// 获取客户端连接
byte[] buffer = new byte[1024];
while (true) {
// 获取客户端请求
DatagramPacket client = new DatagramPacket(buffer, buffer.length);
socket.receive(client);
String request = new String(client.getData(), 0, client.getLength());
// 回显客户端
String response = request;
socket.send(new DatagramPacket(response.getBytes(), response.getBytes().length,
client.getAddress(), client.getPort()));
// 打印交互日志
System.out.printf("客户端[%s:%d] receive-> %s send-> %s\n", client.getAddress().toString(),
client.getPort(), request, response);
}
}
}
UDPClient
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class UDPClient {
public static void main(String[] args) throws IOException {
System.out.println("UdpClient启动");
// 创建客户端套接字
DatagramSocket socket = new DatagramSocket();
// 创建数据报
byte[] buffer = new byte[1024];
DatagramPacket client = new DatagramPacket(buffer, buffer.length,
InetAddress.getByName("127.0.0.1"), 8080);
while (true) {
// 获取发送数据
System.out.print("send -> ");
Scanner scanner = new Scanner(System.in);
String request = scanner.next();
client.setData(request.getBytes());
// 发送数据
socket.send(client);
// 获取服务器回显数据
DatagramPacket server = new DatagramPacket(buffer, buffer.length);
socket.receive(server);
// 打印交互日志
System.out.printf("receive ->[%s:%d] %s\n", server.getAddress().toString(), server.getPort(),
new String(server.getData(), 0, server.getLength()));
}
}
}
测试结果
TCP
TCPServer
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TCPServer {
public static void main(String[] args) throws IOException {
System.out.println("TCPServer启动");
// 创建监听套接字
ServerSocket listenSocket = new ServerSocket(8080, 20, InetAddress.getByName("127.0.0.1"));
// 创建线程池处理客户端数据
ExecutorService clientPool = Executors.newCachedThreadPool();
while (true) {
// 获取客户端连接
Socket client = listenSocket.accept();
// 客户端处理
clientPool.submit(() -> {
try {
clientProcess(client);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
public static void clientProcess(Socket client) throws IOException {
System.out.printf("client[%s:%d] connect success\n",
client.getInetAddress().toString(), client.getPort());
// 处理客户端数据
try (Scanner input = new Scanner(client.getInputStream());
PrintStream output = new PrintStream(client.getOutputStream())) {
while (true) {
if (!input.hasNext()) {
System.out.printf("client[%s:%d] disconnect\n",
client.getInetAddress().toString(), client.getPort());
} else {
// 约定以\n为一个完整报文
String request = input.nextLine();
// 回显客户端
String response = request;
output.println(response);
output.flush();
// 打印交互日志
System.out.printf("client[%s:%d] receive -> %s send -> %s\n",
client.getInetAddress().toString(), client.getPort(), request, response);
}
}
}
}
}
TCPClient
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class TCPClient {
public static void main(String[] args) throws IOException {
System.out.println("TCPClient启动");
// 创建客户端连接
Socket socket = new Socket("127.0.0.1", 8080);
Scanner scanner = new Scanner(System.in);
try (Scanner input = new Scanner(socket.getInputStream());
PrintStream output = new PrintStream(socket.getOutputStream())) {
while (true) {
System.out.print("send -> ");
String request = scanner.nextLine();
// 发送请求数据
output.println(request);
// 获取服务器回显数据
String response = input.nextLine();
// 打印交互日志
System.out.printf("receive -> server[%s:%d] %s\n",
socket.getInetAddress().toString(), socket.getPort(), response);
}
}
}
}
测试结果