【Java】网络编程-UDP字典服务器客户端简单代码编写

2023-12-20 01:59:03

上文讲了UDP回响服务器客户端简单代码编写

本文将讲述UDP字典服务器客户端简单代码编写。所谓回显,就是指客户端向服务器发送一个报文,从服务器那里得到一条一模一样的回响报文

而我们的字典功能呢,则是实现了输入中文,得到对应的英文

1、代码讲解

要实现这个功能,我们只需要对process()方法进行修改,即对处理request,返回response这个功能进行修改

要实现字典功能,我们在构造方法里定义一个Map数据结构,在Map中放入要翻译的词汇

public UdpDictServer(int port) throws SocketException {
        socket = new DatagramSocket(port);
        map.put("小狗","dog");
        map.put("小猫","cat");
        map.put("猪猪","pig");
}

然后对process()方法进行修改

public static String process(String key){
        return map.get(key);
}

其余代码与回响程序相同?

2、完整代码

服务器端完整代码

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

public class UdpDictServer {
    public static DatagramSocket socket = null;
    static Map<String,String> map = new HashMap<>();

    public UdpDictServer(int port) throws SocketException {
        socket = new DatagramSocket(port);
        map.put("小狗","dog");
        map.put("小猫","cat");
        map.put("猪猪","pig");
    }

    public static void start() throws IOException {
        System.out.println("服务器开始!");
        while (true){
            DatagramPacket requestPacket = new DatagramPacket(new byte[1024],1024);
            socket.receive(requestPacket);

            String request = new String(requestPacket.getData(),0,requestPacket.getLength());
            String response = process(request);

            DatagramPacket responsePacket = new DatagramPacket(response.getBytes(),response.getBytes().length, requestPacket.getSocketAddress());
            socket.send(responsePacket);

            System.out.printf("[%s:%d] rec:%s,resp:%s",requestPacket.getAddress().toString(),requestPacket.getPort(),request,response);
        }
    }

    public static String process(String key){
        return map.get(key);
    }

    public static void main(String[] args) throws IOException {
        UdpDictServer server = new UdpDictServer(9090);
        server.start();
    }
}

客户端完整代码

import UDPecho.UdpEchoClient;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class UdpDictClient {
    public static DatagramSocket socket = null;
    private static String ip = null;
    private static int port = 0;

    public UdpDictClient(String ip,int port) throws SocketException {
        socket = new DatagramSocket();
        this.ip = ip;
        this.port = port;
    }

    public static void start() throws IOException {
        System.out.println("客户端开始");
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("->");
            String request = scanner.next();
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(ip),port);
            socket.send(requestPacket);

            DatagramPacket responsePacket = new DatagramPacket(new byte[1024],1024);
            socket.receive(responsePacket);

            String response = new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpDictClient client = new UdpDictClient("127.0.0.1",9090);
        client.start();
    }
}

实现结果

???????

文章来源:https://blog.csdn.net/m0_64921476/article/details/134977886
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。