设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 软件追踪 查看内容

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

2019-8-21 23:29| 发布者: joejoe0332| 查看: 445| 评论: 0|原作者: oschina|来自: oschina

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

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的接口设计,尽量使用户用仅需开发一次便可实现无缝切换通信方式。


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部