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 ()