Java Implementation Ip/tcp Communication helper class Socketsimple

Source: Internet
Author: User
Tags throwable maven central

The beginning of the new Year, work code code, looked at the summary of their own years ago, found that there is a socket communication Help library socketsimple, today to introduce the role of the library.

function explanation

Socketsimple Library is mainly for socket server implementation and socket client implementation of encapsulation, service driven by Serverhelper management, the client is managed by Clienthelper, the communication adopts callback mode, mainly has the exception callback, the connection callback, The message receives the callback, the message sends out the callback and so on, internal multithreading concurrency, the message receives on the independent thread, supports the long connection, supports the multi-service side, the multi-client creation and so on.

Integration method
    • Jar Package Download Socketsimple.jar
    • Maven Central Warehouse http://search.maven.org/Search Socketsimple
      Note: Please use GitHub as the version.
code example
/** * Service-Side test class */public class Test {private static set<string> addresses = new hashset<string> ();         public static void Main (string[] args) {//Create server final Serverhelper helper = Serverhelper.getinstance (true); Helper.createserver (9988, New Servercallback () {//exception callback @Override public void One Rror (Throwable error) {}//Client connection callback @Override public void onaccept (String addr            ESS) {Addresses.add (address);                }//received a client message callback @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);                    Send Message to Client helper.send (9988,next,result);  }}}//Send a successful callback to the client @Override public void onsent (String address,            Byte[] data) {System.out.println (new String);    }        }); }}/** * Client 1 Test class */public class Test1 {public static void main (string[] args) {//Create Client 1 final clienthelp        ER helper = clienthelper.getinstance (true);  Helper.createclient ("192.168.1.114", 9988, New Clientcallback () {//exception callback @Override public void OnError (Throwable e) {}//connection server callback @Override public void onconnected (FIN Al String address, Final int port) {///thread Get keyboard entry (note: Because the keyboard entry will clog the thread, it will cause the method to be blocked when used directly in the method) NE W Thread (New Runnable () {@Override public void run () {Scann ER ScanneR = new Scanner (system.in);                            while (Scanner.hasnextline ()) {String s = scanner.nextline (); if (S.equals ("Exit")) {//Close client helper.closeclient (address,p                                ORT);                                Recycling resources helper.recycle ();                            Return                        }//Send message to Server Helper.send (Address,port,s.getbytes ());            }}). Start ();                 }//Receive Server message callback @Override public void onreceived (String address, int port, byte[] result) {            System.out.println ("client1-received:" + (new String (result));                }//Send Server message succeeded callback @Override public void onsent (String address, int port, byte[] data) { String s = nEW String (data);            System.out.println ("Client1-sent:" + s);    }        }); }}/** * Client 2 Test class */public class Test2 {public static void main (string[] args) {//Create Client 2 final Clienthelp        ER helper = clienthelper.getinstance (false); Helper.createclient ("127.0.0.1", 9988, New Clientcallback () {//Exception callback @Override public Voi D onError (Throwable e) {}//connection server-side callback @Override public void onconnected (final S                    Tring 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                        }//Send message to Server Helper.send (Address,port,s.getbytes ());            }}). Start ();                 }//Receive server-side message callback @Override public void onreceived (String address, int port, byte[] result) {            System.out.println ("client2-received:" + (new String (result));                }//Send a successful callback to the server @Override public void onsent (String address, int port, byte[] data) {            System.out.println ("Client2-sent:" + (new String (data)); }        });}}

Note: The server handles two client chat messages and two client chat samples.

Conclusion

Welcome to the integration of use, if the source of interest, please visit GitHub address, if it feels good, please give a star to encourage, the use of problems please leave issues. If you have better ideas or suggestions and good inspiration, please email author, thank you!

    • Qqemail:[email protected]

    • Gmail:[email protected]

    • 163Email:[email protected]

Java Implementation Ip/tcp Communication helper class Socketsimple

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.