2013-02-01 159 views
0

我正在研究一個非常簡單的Java客戶端/服務器系統(只是爲了讓我的腳溼透套接字)。出於某種原因,我不斷收到 「套接字已關閉」 的錯誤...這裏是我的代碼..Java客戶端服務器「套接字已關閉」

服務器文件

public class Server { 

    public static ServerSocket s = null; 

    public static void main(String[] args) { 
     //Create the server socket 
     int port = 1111; 
     if (args.length > 0) { 
      if (isInt(args[0]) && Integer.parseInt(args[0]) < 65537) { 
       port = Integer.parseInt(args[0]); 
      } 
     } 
     try { 
      s = new ServerSocket(port); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      s.setSoTimeout(0); 
     } catch (SocketException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     Runnable r = new Runnable() { 
      public void run() { 
       while (true) { 
        Socket caught = null; 
        try { 
         caught = Server.s.accept(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        if (caught == null) { 
         return; 
        } 

        InputStream is = null; 
        try { 
         is = caught.getInputStream(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
        try { 
         String output; 
         while ((output = br.readLine()) != null) { 
          handleCommand(output, caught); 
         } 
        } catch (Exception e) { 
        } 
        try { 
         br.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
     }; 

     Thread t = new Thread(r); 
     t.start(); 
    } 

    public static boolean isInt(String in) { 
     try { 
      Integer.parseInt(in); 
      return true; 
     } catch (Exception e) { 
      return false; 
     } 
    } 

    public static void handleCommand(String in, Socket s1) { 
     if (in.equalsIgnoreCase("test")) { 
      System.out.println("Recieved Test Command.."); 
      System.out.println("Sending response.."); 
      PrintStream ps = null; 
      try { 
       ps = new PrintStream(s1.getOutputStream(), true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      ps.close(); 
     } 
    } 
} 

客戶端文件

public class Client { 

    public static Socket s = null; 

    public static void main(String[] args) { 
     int port = 1111; 
     String server = "localhost"; 
     if (args.length > 0) { 
      if (isInt(args[0]) && Integer.parseInt(args[0]) < 65537) { 
       port = Integer.parseInt(args[0]); 
      } 
     } 
     if (args.length > 1) { 
      server = args[1]; 
     } 


     try { 
      s = new Socket(server, port); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     if (s != null) { 

      Runnable r = new Runnable() { 
       public void run() { 
        while (true) { 
         InputStream is = null; 
         try { 
          is = s.getInputStream(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
         try { 
          String output; 
          while ((output = br.readLine()) != null) { 
           System.out.println(output); 
          } 
         } catch (Exception e) { 
         } 
         try { 
          br.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       } 
      }; 

      Thread t = new Thread(r); 
      t.start(); 

      PrintStream ps = null; 
      try { 
       ps = new PrintStream(s.getOutputStream(), true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      System.out.println("Sending Test message.."); 
      try { 

       ps.println("test"); 

      } catch (Exception e) { 
       System.out.println("Error: - " + e.getMessage()); 
      } 


     } 
    } 

    public static boolean isInt(String in) { 
     try { 
      Integer.parseInt(in); 
      return true; 
     } catch (Exception e) { 
      return false; 
     } 
    } 
} 

我在第41行客戶端中獲取錯誤,然後在第46行發生NullPointerException ..

Thanks in adv任何幫助。我只是想在這裏學習。

+0

你可能想看看[this](http://stackoverflow.com/questions/14617331/java-socket-swingworker-running-but-no-message-received-or-transmitted/14617650#14617650 ) – MadProgrammer

+0

你的異常處理已經到了極點。太多的本地捕獲塊跟隨無條件達到的活動代碼。重組所有這些。 – EJP

回答

0

在第61行的服務器上,當您第一次閱讀時,您的客戶端沒有機會發送數據,因此它不會停在循環中,並向前移動以關閉第68行中的閱讀器。

嘗試創建一個類來處理在服務器上,這使得更容易想到的服務器做什麼,像ClientHandler的將是一個不錯的選擇傳入連接;)

有樂趣!

相關問題