smart-socket v1.4.4 Bate 版发布,迟到的 UDP 通信

来源: 投稿
2019-08-21 13:38:00

smart-socket 是一款国产开源的 Java AIO 框架,追求代码量、性能、稳定性、接口设计各方面都达到极致。如果 smart-socket 对您有一丝帮助,请 Star 一下我们的项目并持续关注;如果您对 smart-socket 并不满意,那请多一些耐心,smart-socket 一直在努力变得更好。

本次 beta 版主要为 smart-socket 提供 UDP 通信服务的能力,这也是众多用户心心念念的功能。我们先通过一个简单的 demo了解下如何使用 smart-socket 进行 UDP 的通信开发。

public class UdpDemo {
    public static void main(String[] args) throws IOException, InterruptedException {

        //服务端
        final UdpBootstrap<String, String> bootstrap = new UdpBootstrap<String, String>(new StringProtocol(), new MessageProcessor<String, String>() {
            @Override
            public void process(UdpChannel<String, String> channel, SocketAddress remote, String msg) {
                InetSocketAddress remoteAddress = (InetSocketAddress) remote;
                if (remoteAddress.getPort() == 9999) {
                    System.out.println(channel + " receive response:" + msg);
                } else {
                    System.out.println("server receive request:" + msg);
                    try {
                        channel.write(msg, remote);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        bootstrap.open(9999);
        System.out.println("启动成功");

        //客户端
        int i = 10;
        final SocketAddress remote = new InetSocketAddress("localhost", 9999);
        while (i-- > 0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int count = 10;
                        UdpChannel<String, String> channel = bootstrap.open();
                        while (count-- > 0) {
                            channel.write("HelloWorld", remote);
                        }
                        System.out.println("发送完毕");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }
        Thread.sleep(100);
        bootstrap.shutdown();
    }
}

对于 udp 的支持会延续 smart-socket 的一贯风格:极简、易用、高性能,感兴趣的朋友可以下载代码体验体验(开发分支:https://gitee.com/smartboot/smart-socket/tree/1.0.0-DEV/)。

至于正式版的发布日期待定,因为smart-socket期望整合TCP、UDP的接口设计,尽量使用户用仅需开发一次便可实现无缝切换通信方式。

展开阅读全文
点击加入讨论🔥(7) 发布并加入讨论🔥
本篇精彩评论
ip:220.129.233.153
port:786
pwd:mimabubian
chacha20
2019-08-21 15:07
1
举报
7 评论
21 收藏
分享
返回顶部
顶部