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

栏目: 软件资讯 · 发布时间: 4年前

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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Spark SQL内核剖析

Spark SQL内核剖析

朱锋、张韶全、黄明 / 电子工业出版社 / 2018-8 / 69.00元

Spark SQL 是 Spark 技术体系中较有影响力的应用(Killer application),也是 SQL-on-Hadoop 解决方案 中举足轻重的产品。《Spark SQL内核剖析》由 11 章构成,从源码层面深入介绍 Spark SQL 内部实现机制,以及在实际业务场 景中的开发实践,其中包括 SQL 编译实现、逻辑计划的生成与优化、物理计划的生成与优化、Aggregation 算......一起来看看 《Spark SQL内核剖析》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码