Java code implements socket interface communication (blocking I/O)

Source: Internet
Author: User
Tags readline

The traditional Java implementation of socket communication is relatively simple implementation, but it is a plug-in I/O stream access, only one thread to complete the current task to start the next thread, unable to resolve the high concurrency;

1, the Simple Socketservice

A handler processing thread is established for each socket connection, and the processing thread processes the data in the InputStream stream, sends the processing result through PrintWriter to the client, and finally closes the input stream, The output stream and socket socket handle resources.

1  Public classTimeserver {2 3      Public Static voidMain (string[] args)throwsIOException {4 5        intPort = 8080;//server port with 80806ServerSocket Server =NULL;7Socket socket =NULL;8        Try {9Server =NewServerSocket (port);Ten             while(true){ OneSocket =server.accept (); A                NewThread (NewTimehandler (socket)). Start (); -            } -}Catch(Exception e) { the e.printstacktrace (); -}finally{ -            if(server!=NULL){ - server.close (); +Server =NULL; -System.out.println ("The time server close ..."); +            } A        } at     } -  - } -  - classTimehandlerImplementsrunnable{ -  in     Privatesocket socket; -  to      PublicTimehandler () { +  -     } the      PublicTimehandler (socket socket) { *          This(); $          This. Socket =socket;Panax Notoginseng     } -  the      Public voidrun () { +System.out.println (Thread.CurrentThread (). GetName () + "Start success ..."); ABufferedReader in =NULL; thePrintWriter out =NULL; +         Try { -in =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); $out =NewPrintWriter (Socket.getoutputstream (),true); $              while(true){ -String str =in.readline (); -                 if("End". Equals (str)) { the                      Break; -                 }WuyiString time = str+ ":" +System.currenttimemillis (); the out.println (time); -             } Wu}Catch(Exception e) { - e.printstacktrace (); About}finally{ $             if(in!=NULL){ -                 Try { - in.close (); -}Catch(IOException e) { A e.printstacktrace (); +                 } thein =NULL; -             } $             if(out!=NULL){ the out.close (); theout=NULL; the             } the             if(socket!=NULL){ -                 Try { in socket.close (); the}Catch(IOException e) { the e.printstacktrace (); About                 } theSocket =NULL; the             } the         } +  -     } the}

2, Socketclient

1  Public classtimeclient {2 3      Public Static voidMain (string[] args) {4         intPort = 8080;5Socket socket =NULL;6BufferedReader in =NULL;7PrintWriter out =NULL;8 9         Try {TenSocket =NewSocket ("127.0.0.1", port); Onein =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); Aout =NewPrintWriter (Socket.getoutputstream (),true); -Out.println ("Hello"); -OUT.PRINTLN ("World"); theOut.println ("End"); -  -SYSTEM.OUT.PRINTLN ("Send Message success ..."); -              while(true){ +String res =in.readline (); -                 if("". Equals (res) | | res==NULL){ +                      Break; A                 } atSYSTEM.OUT.PRINTLN ("The res is:" +res); -             } -  -  -}Catch(Exception e) { - e.printstacktrace (); in}finally{ -             if(in!=NULL){ to                 Try { + in.close (); -}Catch(IOException e) { the e.printstacktrace (); *                 } $in =NULL;Panax Notoginseng             } -             if(out!=NULL){ the out.close (); +out=NULL; A             } the             if(socket!=NULL){ +                 Try { - socket.close (); $}Catch(IOException e) { $ e.printstacktrace (); -                 } -Socket =NULL; the             } -         }Wuyi  the     } -  Wu}

Java code implements socket interface communication (blocking I/O)

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.