Socket Programming Bufferreader.readline () encountered by the pit __ programming

Source: Internet
Author: User
Tags readline

Socket Programming Bufferreader.readline () The pits encountered

The last few days the company will start a project need to use Java docking hardware, is a car park Intelligent management Project, in fact, to communicate with the hardware, heard Jboos netty good. On the study of two days did not research what results come, may be their own ability, but see someone said "Netty authoritative guide" this book, so download looked, because the feeling now Baidu on the Entty article information is not much, also not comprehensive and careful. Okay, so get to the point, and when you look at chapter two, the author introduces synchronous blocking IO, there is an example. This is the case in the book.

public class Timeserver {public static void main (string[] args) throws IOException {int port = 8085;
        ServerSocket server = null;
            try {//Create service server = new ServerSocket (port);
            System.out.println ("The time server was start in port:" +port);
            Socket socket = NULL; 
                while (true) {//Listening socket = server.accept ();
            New Thread (new Timeserverhandler (socket)). Start ();
        The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
                } finally{if (server!= null) {server.close ();
                System.out.println ("The time server is close!");
            Server = NULL;

    Class Timeserverhandler implements runnable{private socket socket = NULL; Public Timeserverhandler (socket socket) {This.socket = socket;
        @Override public void Run () {//character read cache stream BufferedReader in = null;
        PrintWriter out = null;
            try {in = new BufferedReader (New InputStreamReader (This.socket.getInputStream ()));
            out = new PrintWriter (New OutputStreamWriter (This.socket.getOutputStream ()), true);
            String currenttime = null;
            String BODY = null;
                while (true) {BODY = In.readline ();
                if (BODY = = null) {break;
                } System.out.println ("The time server receive order:" +body); If the client sends a command that returns the current time when the QUERY times order, otherwise the bad order instruction is returned invalid currenttime = "Query time orders". Equalsignore
            Case (body), new Date (). toString (): "Bad order";
        }//Response client Out.print (currenttime);
        catch (Exception e) {e.printstacktrace ();
              } finally{if (in!= null) {  try {in.close ();
                catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
            } in = null;
                } if (out!= null) {out.close ();
            out = null;
                } if (socket!= null) {try {this.socket.close ();
                catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
            } this.socket = null; }}} public class Timeclient {public static void main (string[] args) {Socket socket = n
        ull;
        BufferedReader in = null;
        PrintWriter out = null;
            try {socket = new socket ("Rascal-guo", 8085);
            in = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); out = NEW PrintWriter (New OutputStreamWriter (Socket.getoutputstream ()), true);
            Out.println ("QUERY Time Order");
            SYSTEM.OUT.PRINTLN ("Send order to Server succeed!");
            String resp = In.readline ();
        System.out.println ("Now:" +resp);
        catch (Exception e) {e.printstacktrace ();
                } finally{if (out!= null) {out.close ();
            out = null;
                } if (in!= null) {try {in.close ();
                catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
            } in = null;
                } if (socket!= null) {try {socket.close ();
                catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
              }  socket = NULL; }
        }
    }
}

There is a problem when running, Timeserverhandler class = In.readline (); This line of code was first executed, but it was blocked for the second time, and it was never executed. After the search on the Internet, a variety of statements collected, and then with their own code inspiration for the gradual trial modification, first of all in the Timeclient class Out.println ("QUERY time order\n"); followed by a newline character, because the In.readline () is read-by-line (according to the newline character), and the server may not read the line break, so it reads (...). It seems to be a bit out of the sense. Run down the server and the client again. Discover that the service-side ReadLine () method is not blocked. But the second time he would read the "empty string" and not understand it. Since the program does not affect the smooth implementation of the matter, the first problem is solved, but the second question comes again. The client print Service side response is Null,now time is:null. Puzzled by the solution. According to the information I saw earlier. Sensory to the service End Out.print (currenttime) followed by a out.flush (); The problem was solved successfully. As for why, because the younger brother knowledge is shallow, did not study the source code, therefore cannot the profound understanding, above if has the understanding mistake, looks correct.

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.