C/S基本架构

文章目录

UDP

对于客户端/服务器之间的一个交互,首先我们可以写一个回显服务器来展示下基本架构;客户端为请求方,而服务器为响应方;客户端为主动方,而服务器为被动方。
下面是服务器的响应程序。

public class UdpEchoserver {
    private DatagramSocket socket=null; //构造一个DatagramPacket对象,并置为空,相当于打开一个文件

    public UdpEchoserver(int port) throws SocketException {    //构造方法
         socket=new DatagramSocket(port);
    }
    //启动服务器
    public void start() throws IOException { //成员方法
        System.out.println("服务器启动!");
        while (true){
//            1.尝试读取客户端发来的请求
            DatagramPacket requestPacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(requestPacket);
//            2.对请求进行解析,把DatagramPacket对象转成String
            String request=new String(requestPacket.getData(),0,requestPacket.getLength());
//            3.根据请求处理响应
            String response =process(request);
//            4.把响应构造成DatagramPack对象
            DatagramPacket responsePacket =new DatagramPacket
                    (response.getBytes(),response.getBytes().length,requestPacket.getSocketAddress());
//            5.把这个DatagramPacket对象返回给客户端
            socket.send(responsePacket);
            System.out.printf("[%s:%d] req=%s; resp=%s\n",requestPacket.getAddress().toString(),
                    requestPacket.getPort(),request,response);
        }
    }
    public String process(String req){
        return req;
    }

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

下面为客户端的请求程序

public class UdpEchoclient {
    private static DatagramSocket socket=null;
    public UdpEchoclient() throws SocketException {
        socket=new DatagramSocket();
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        while (true){
//        1.让客户端从控制台读取一个请求数据
            System.out.println(">");
            String request =scanner.next();
//        2.把这个字符请求发送给服务器,构造DatagramPacket对象;构造的Packet即含数据又含地址
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName("127.0.0.1"),8000);
//        3.把数据报发送给服务器
            socket.send(requestPacket);
//        4.从服务器读取相应数据
            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
//        5.把响应数据获取出来,转成字符串
            String response =new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.printf("req: %s;rep: %s\n",request,response);
        }
    }
    public static void main(String[] args) throws IOException {
        UdpEchoclient client=new UdpEchoclient();
        client.start();
    }
}

我们启动程序,就可以看到回显服务器的结果
在这里插入图片描述

翻译服务器

public class UdpdicServer extends UdpEchoserver{
    private Map<String,String> dict =new HashMap<>();

    public UdpdicServer(int port) throws SocketException {
        super(port);
        dict.put("cat","小猫");
        dict.put("dog","小狗");
        dict.put("fuck","卧槽");
    }
    public String process(String req){
        return dict.getOrDefault(req,"输入单词错误");
    }

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

在上述代码中,简单列举了几个单词,运行如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值