Java實現IP/TCP通訊協助類SocketSimple

來源:互聯網
上載者:User

標籤:回收   聊天   ntc   鼓勵   ref   print   void   用戶端串連   sim   

新春伊始,上班碼代碼,看了一下自己年前的總結,發現有一個Socket通訊的協助庫SocketSimple,今天就介紹一下該庫的作用。

作用講解

SocketSimple庫主要是對Socket服務端實現及Socket用戶端實現的封裝,服務端由ServerHelper管理,用戶端由ClientHelper管理;通訊均採用回調模式,主要有異常回調、串連回調、訊息接收回調、訊息發出回調等;內部多線程並發,訊息接收在獨立線程,支援長串連,支援多服務端、多用戶端建立等。

整合方式
  • Jar包下載SocketSimple.jar
  • Maven中央倉庫http://search.maven.org/搜尋 SocketSimple
    Note:使用時版本請以Github為準。
程式碼範例
/** * 服務端測試類別 */public class Test {    private static Set<String> addresses = new HashSet<String>();    public static void main(String[] args) {        //建立伺服器        final ServerHelper helper = ServerHelper.getInstance(true);        helper.createServer(9988, new ServerCallBack() {            //異常回調            @Override            public void onError(Throwable error) {            }            //用戶端串連回調            @Override            public void onAccept(String address) {                addresses.add(address);            }            //收到用戶端訊息回調            @Override            public void onReceived(String address, byte[] result) {                String x = new String(result);                Iterator<String> iterator = addresses.iterator();                while (iterator.hasNext()) {                    String next = iterator.next();                    if (!next.equals(address)) {                        System.out.println(x);                        System.out.println(next);                        //向用戶端發送訊息                        helper.send(9988,next,result);                    }                }            }            //向用戶端發送成功回調            @Override            public void onSent(String address, byte[] data) {                System.out.println(new String(data));            }        });    }}/** * 用戶端1測試類別 */public class Test1 {    public static void main(String[] args) {        //建立用戶端1        final ClientHelper helper = ClientHelper.getInstance(true);        helper.createClient("192.168.1.114", 9988, new ClientCallBack() {            //異常回調            @Override            public void onError(Throwable e) {            }            //串連伺服器回調            @Override            public void onConnected(final String address, final int port) {                //開線程擷取鍵盤錄入(注意:因為鍵盤錄入會堵塞線程,所以當直接在該方法使用時會導致該方法所線上程堵塞)                new Thread(new Runnable() {                    @Override                    public void run() {                        Scanner scanner = new Scanner(System.in);                        while (scanner.hasNextLine()) {                            String s = scanner.nextLine();                            if (s.equals("exit")) {                                //關閉用戶端                                helper.closeClient(address,port);                                //回收資源                                helper.recycle();                                return;                            }                            //向伺服器發送訊息                            helper.send(address,port,s.getBytes());                        }                    }                }).start();            }            //接收伺服器訊息回調            @Override            public void onReceived(String address, int port, byte[] result) {                System.out.println("Client1-Received: " + (new String(result)));            }            //發送伺服器訊息成功回調            @Override            public void onSent(String address, int port, byte[] data) {                String s = new String(data);                System.out.println("Client1-Sent: " + s);            }        });    }}/** * 用戶端2測試類別 */public class Test2 {    public static void main(String[] args) {        //建立用戶端2        final ClientHelper helper = ClientHelper.getInstance(false);        helper.createClient("127.0.0.1", 9988, new ClientCallBack() {            //異常回調            @Override            public void onError(Throwable e) {            }            //串連服務端回調            @Override            public void onConnected(final String address, final int port) {                new Thread(new Runnable() {                    @Override                    public void run() {                        Scanner scanner = new Scanner(System.in);                        while (scanner.hasNextLine()) {                            String s = scanner.nextLine();                            if (s.equals("exit")) {                                helper.closeClient(address,port);                                helper.recycle();                                return;                            }                            //向服務端發送訊息                            helper.send(address,port,s.getBytes());                        }                    }                }).start();            }            //接收服務端訊息回調            @Override            public void onReceived(String address, int port, byte[] result) {                System.out.println("Client2-Received: " + (new String(result)));            }            //向服務端發送成功回調            @Override            public void onSent(String address, int port, byte[] data) {                System.out.println("Client2-Sent: " + (new String(data)));            }        });}}

Note:服務端處理兩個用戶端的聊天資訊,兩個用戶端聊天樣本。

結語

歡迎大家整合使用,如果對源碼感興趣,請移步Github地址,若感覺好用,請給個Star鼓勵一下,使用中遇到問題請留下issues。大家如果有更好的意見或建議以及好的靈感,請郵箱作者,謝謝!

  • qqEmail :[email protected]

  • gmail :[email protected]

  • 163Email:[email protected]

Java實現IP/TCP通訊協助類SocketSimple

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.