BufferedReader的readLine()为非阻塞

来源:互联网 发布:ubuntu用什么聊天工具 编辑:程序博客网 时间:2024/05/22 04:26
  BufferedReader的readLine()为非阻塞,阻塞部分主要在Socket clientSocket = serverSocket.accept(); 
public class ShutDownServer {        static ServerSocket serverSocket = null;// 服务socket        public static void main(String[] args) {          try {              // 监听30000端口          System.out.println("before ServerSocket  "+new Date());             serverSocket = new ServerSocket(30000);              System.out.println("ShutDownServer is listening  port 30000............");              System.out.println("after ServerSocket  "+new Date());             int i=0;          //  while (true) {                  // 获取客户端套接字                               Socket clientSocket = serverSocket.accept();                                  try {                                                   BufferedReader input=null;                                input=new BufferedReader(new InputStreamReader            (clientSocket.getInputStream()));                          while(true){                     //  System.out.println("阻塞到了input.readLine()");              //while(input.ready()){                                        String msg = input.readLine();                              System.out.println("msg is  "+msg);                            System.out.println("i is  "+i++);                                                      //input.readLine()可以读到空值                          //  System.out.println("收到的第"+i+"个命令"+msg);                                                                                     if ("shutdown".equals(msg)) {                                  shutdown(clientSocket);                                  System.out.println("shutdown执行");                                                                                            } else if ("reboot".equals(msg)) {                                  reboot();                                                             }                             else if ("cancel".equals(msg)) {                                  cancel(clientSocket);                                  System.out.println("cancel执行");                                                          //  }              }                        }                } catch (Exception e) {                  } finally {                      try {                          clientSocket.close();                      } catch (IOException e) {                          e.printStackTrace();                      }                  }          //    }          } catch (Exception e) {              e.printStackTrace();          }      }        // 关机      private  static void shutdown( Socket clientSocket) throws IOException {        //  Runtime.getRuntime().exec("shutdown -s -t 60");        Date dNow = new Date();           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         String Datenow = sdf.format(dNow);           System.out.println("shutdown ,60 seconds later from"+Datenow);     PrintStreamout=new PrintStream(clientSocket.getOutputStream());    out.println("shutdown 已经执行");    }        // 重启      private static void reboot() throws IOException {          Runtime.getRuntime().exec("shutdown -r -t 60");          System.out.println("reboot ,60 seconds later ");      }        // 取消关机或重启      private static void cancel(Socket clientSocket) throws IOException {       //   Runtime.getRuntime().exec("shutdown -a");          Date dNow = new Date();           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         String Datenow = sdf.format(dNow);           System.out.println("cancel shutdown or restart"+Datenow);      PrintStreamout=new PrintStream(clientSocket.getOutputStream());    out.println("cancel 已经执行");    }    }  

所以在取值的时候应该先判断

while(input.ready()){                                        String msg = input.readLine();                              System.out.println("msg is  "+msg);                            System.out.println("i is  "+i++);                            ;                          //  System.out.println("收到的第"+i+"个命令"+msg);                                                         // 判断输入,进行相应的操作                              if ("shutdown".equals(msg)) {                                  shutdown(clientSocket);                                  System.out.println("shutdown执行");                                // 发送消息回Android端                                                                                        } else if ("reboot".equals(msg)) {                                  reboot();                                                             }                             else if ("cancel".equals(msg)) {                                  cancel(clientSocket);                                  System.out.println("cancel执行");                                                           }              }            
客户端部分:

public class client{public static void main(String[]args) throws Exception{Socket client=null; Thread.sleep(5000);client=new Socket("127.0.0.1",30000);PrintStream out=null;BufferedReader input=null;out=new PrintStream(client.getOutputStream());    Thread.sleep(5000);String str="shutdown"; System.out.println("before write  "+new Date());out.println(str); System.out.println("after write  "+new Date());System.out.println("发送了shutdown");String str1="cancel";out.println(str1); System.out.println("after write2  "+new Date());System.out.println("发送了cancel");input=new BufferedReader(new InputStreamReader(client.getInputStream()));String msg = input.readLine(); System.out.println(msg);String msg1 = input.readLine(); System.out.println(msg1);   client.close();}}