Usage of client and server objects


When the YMP framework starts, it will automatically scan and load classes declared with @Server and @Client annotations, and configure the client based on the annotation settings and corresponding parameters. Or the initialization of the server object, but the client and server programs are not executed directly at this time, and the startup action needs to be completed manually. The code is as follows:

  • Example 1: Start all loaded Client and server services

    public static void main(String[] args) throws Exception {
        YMP.get().init();
        //
        Servs.get().startup();
    }
  • Example 2: Get the specified client or server service, start the service and send it to the service The end sends a message

    public static void main(String[] args) throws Exception {
        YMP.get().init();
    
        // 获取服务端实例对象
        NioUdpServer _serv = Servs.get().getServer(UdpServer.class);
        // 启动服务
        _serv.start();
    
        // 获取客户端实例对象
        NioUdpClient _c = Servs.get().getClient(UdpClient.class);
        // 连接到远程服务
        _c.connect();
        // 通过客户端对象向服务端发送消息
        _c.send("Message from Client.");
    }

    Note: During the YMP framework initialization process, if try...finally is used to execute YMP.get() .destroy() destruction action, the service will be stopped just after it is started.