1、回显服务器
服务器端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class UDP_Server {private DatagramSocket socket=null;public UDP_Server(int port) throws SocketException {//手动指定端口socket=new DatagramSocket(port);//系统自动分配端口/*socket=new DatagramSocket();*/}public void start() throws IOException {//start方法启动服务器System.out.println("服务器启动");while (true){//1.读取请求DatagramPacket requestPacket=new DatagramPacket(new byte[2000],2000);socket.receive(requestPacket);//receive 后接收到的是二进制,需要转成字符串String request=new String(requestPacket.getData(),0,requestPacket.getLength());//2.根据请求计算响应,由于这里是回显服务器所以原样子返回String response=process(request);//3.把响应返回客户端,在DatagramPacket构造在send返回DatagramPacket reponsePacket=new DatagramPacket(response.getBytes(), response.getBytes().length,requestPacket.getSocketAddress());socket.send(reponsePacket);//4.打印日志,交互详情System.out.printf("[%s:%d]req:%s,resp=%s\n",requestPacket.getAddress().toString(),requestPacket.getPort(),request,response);}}private String process(String request) {return request;}public static void main(String[] args) throws IOException {UDP_Server udpServer=new UDP_Server(900);udpServer.start();}
}
客户端
import java.io.IOException;
import java.net.*;
import java.util.Scanner;public class UDP_Client {private DatagramSocket socket=null;private String serverIp;private int serverPort;public UDP_Client(String ip,int port) throws SocketException {//创建对象,不能手动指定窗口socket=new DatagramSocket();//把对端的情况记录下来serverIp=ip;serverPort=port;}public void start() throws IOException {System.out.println("客户端启动");Scanner scanner=new Scanner(System.in);while (true){//1。从控制台读取数据,作为请求System.out.print("->");String request=scanner.next();//2.把请求内容构造成DatagramPacket对象,发给服务器DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,InetAddress.getByName(serverIp),serverPort);socket.send(requestPacket);//3.读取服务器返回信息DatagramPacket responsePacket=new DatagramPacket(new byte[2000],2000);socket.receive(responsePacket);//4.转换成字符串输出String reponse=new String(responsePacket.getData(),0, responsePacket.getLength());System.out.println(reponse);}}public static void main(String[] args) throws IOException {UDP_Client udpClient=new UDP_Client("127.0.0.1",900);udpClient.start();}
}
2、翻译服务器
服务器端
import java.util.Map;public class UDP_translate_Server extends UDP_Server{private Map<String,String> map=new HashMap<>();public UDP_translate_Server(int port) throws SocketException {super(port);map.put("小狗","dog");map.put("小猫","cat");map.put("小猪","pig");map.put("小老鼠","mouse");}@Overridepublic String process(String request) {return map.get(request);}public static void main(String[] args) throws IOException {UDP_translate_Server test=new UDP_translate_Server(900);test.start();}
}
客户端
import java.io.IOException;
import java.net.*;
import java.util.Scanner;public class UDP_Client {private DatagramSocket socket=null;private String serverIp;private int serverPort;public UDP_Client(String ip,int port) throws SocketException {//创建对象,不能手动指定窗口socket=new DatagramSocket();//把对端的情况记录下来serverIp=ip;serverPort=port;}public void start() throws IOException {System.out.println("客户端启动");Scanner scanner=new Scanner(System.in);while (true){//1。从控制台读取数据,作为请求System.out.print("->");String request=scanner.next();//2.把请求内容构造成DatagramPacket对象,发给服务器DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,InetAddress.getByName(serverIp),serverPort);socket.send(requestPacket);//3.读取服务器返回信息DatagramPacket responsePacket=new DatagramPacket(new byte[2000],2000);socket.receive(responsePacket);//4.转换成字符串输出String reponse=new String(responsePacket.getData(),0, responsePacket.getLength());System.out.println(reponse);}}public static void main(String[] args) throws IOException {UDP_Client udpClient=new UDP_Client("127.0.0.1",900);udpClient.start();}
}