歡迎您光臨本站 註冊首頁

smart-socket v1.4.4 Bate 版發布,遲到的 UDP 通信

←手機掃碼閱讀     admin @ 2019-08-21 , reply:0

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的介面設計,盡量使用戶用僅需開發一次便可實現無縫切換通信方式。


[admin ]

來源:OsChina
連結:https://www.oschina.net/news/109245/smart-socket-1-4-4-beta-released
smart-socket v1.4.4 Bate 版發布,遲到的 UDP 通信已經有70次圍觀

http://coctec.com/news/all/show-post-212558.html