Understanding of hasnextline ()

Source: Internet
Author: User

The previous understanding of hasnextline is: determine whether there is a next value

Today I found a special case. It turns out to be a blocking method.

Take a look at the following case

This is the server

Package Service; import Java. io. ioexception; import Java. io. printwriter; import java.net. serversocket; import java.net. socket; import Java. util. optional; public class service_1 {public static void main (string [] ARGs) throws ioexception {serversocket Ss = new serversocket (9999); system. out. println ("I Am a server" + SS. getinetaddress (); Response SC = NULL; printwriter PW = NULL; int I = 1; while (true) {socket S = ss. accept (); system. out. println ("there is a port connection up" + S. getinetaddress (); // get the input stream SC = new second (S. getinputstream (); PW = new printwriter (S. getoutputstream (); // PW. println ("I am server" + I); // PW. flush (); // system. out. println (SC. hasnextline (); // if this line is added here, the blocked do {PW. println ("I am server" + I); PW. flush (); If (SC. hasnextline () {system. out. println ("this client says to me:" + SC. nextline () ;} I ++ ;}while (true );}}}

Client

Import Java. io. ioexception; import Java. io. printwriter; import java.net. socket; import java.net. unknownhostexception; import Java. util. role; public class testclient4 {/*** @ Param ARGs * @ throws ioexception * @ throws */public static void main (string [] ARGs) throws ioexception {socket S = new socket ("localhost", 9999); system. out. println ("Client Connected" + S. getlocalport (); Nation SC = new region (S. getinputstream (); printwriter PW = new printwriter (S. getoutputstream (); // The while (SC. hasnextline () {string line = SC. nextline (); line = new string (line. getbytes (), "UTF-8"); system. out. println ("server" + S. getinetaddress () + "client description" + line); If ("bye ". equals (line) {system. out. println ("server" + S. getinetaddress () + "disconnected from the client"); S. close (); break;} // reply Server String response = talk (S. getinetaddress (). tostring (); PW. println (response); PW. flush (); If ("bye ". equals (response) {system. out. println ("the client actively disconnects from the server"); S. close (); break ;}}public static string talk (string client) {consumer SC = new consumer (system. in); system. out. println ("client expression:"); string line = SC. nextline (); return line ;}}

 

Understanding of hasnextline ()

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.