“line“

服务端

package work;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.List;
 
public class UDPServer {
    private static final int PORT = 9876;
    private static List clients = new ArrayList<>();
 
    public static void main(String[] args) {
        try {
            DatagramSocket socket = new DatagramSocket(PORT);
 
            System.out.println("UDP 服务器在端口上运行 " + PORT);
 
            while (true) {
                byte[] receiveData = new byte[1024];
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                socket.receive(receivePacket);
 
                String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
                System.out.println("收到来自 " + receivePacket.getAddress() + ": " + message);
 
                // Broadcast message to all clients
                broadcastMessage(message, receivePacket.getAddress(), receivePacket.getPort());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static void broadcastMessage(String message, java.net.InetAddress address, int port) {
        for (ClientInfo client : clients) {
            try {
                DatagramSocket clientSocket = new DatagramSocket();
                byte[] sendData = message.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, client.getAddress(), client.getPort());
                clientSocket.send(sendPacket);
                clientSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private static class ClientInfo {
        private java.net.InetAddress address;
        private int port;
 
        public ClientInfo(java.net.InetAddress address, int port) {
            this.address = address;
            this.port = port;
        }
 
        public java.net.InetAddress getAddress() {
            return address;
        }
 
        public int getPort() {
            return port;
        }
    }
}

客服端

package work;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
 
public class TCPClient {
 
    public static void main(String[] args) {
        final String serverAddress = "localhost";
        final int serverPort = 12345;
 
        try (Socket socket = new Socket(serverAddress, serverPort)) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
 
            // 从服务器接收消息并显示
            new Thread(() -> {
                try {
                    String message;
                    while ((message = reader.readLine()) != null) {
                        System.out.println("服务器: " + message);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
 
            // 从控制台读取输入并发送给服务器
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
            String consoleInput;
            while ((consoleInput = consoleReader.readLine()) != null) {
                writer.println(consoleInput);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 “line“_第1张图片

客户端

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.List;
 
public class UDPServer {
    private static final int PORT = 9876;
    private static List clients = new ArrayList<>();
 
    public static void main(String[] args) {
        try {
            DatagramSocket socket = new DatagramSocket(PORT);
 
            System.out.println("UDP Server is running on port " + PORT);
 
            while (true) {
                byte[] receiveData = new byte[1024];
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                socket.receive(receivePacket);
 
                String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
                System.out.println("Received from " + receivePacket.getAddress() + ": " + message);
 
                // Broadcast message to all clients
                broadcastMessage(message, receivePacket.getAddress(), receivePacket.getPort());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static void broadcastMessage(String message, java.net.InetAddress address, int port) {
        for (ClientInfo client : clients) {
            try {
                DatagramSocket clientSocket = new DatagramSocket();
                byte[] sendData = message.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, client.getAddress(), client.getPort());
                clientSocket.send(sendPacket);
                clientSocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private static class ClientInfo {
        private java.net.InetAddress address;
        private int port;
 
        public ClientInfo(java.net.InetAddress address, int port) {
            this.address = address;
            this.port = port;
        }
 
        public java.net.InetAddress getAddress() {
            return address;
        }
 
        public int getPort() {
            return port;
        }
    }
}

服务端

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
 
public class UDPClient {
    private static final int PORT = 9876;
 
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter your name: ");
            String name = scanner.nextLine();
 
            DatagramSocket socket = new DatagramSocket();
            InetAddress serverAddress = InetAddress.getByName("localhost");
 
            Thread receiveThread = new Thread(() -> {
                try {
                    byte[] receiveData = new byte[1024];
                    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
 
                    while (true) {
                        socket.receive(receivePacket);
                        String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
                        System.out.println(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
 
            receiveThread.start();
 
            while (true) {
                String message = scanner.nextLine();
                String formattedMessage = name + ": " + message;
                byte[] sendData = formattedMessage.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, PORT);
                socket.send(sendPacket);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(eclipse)