Java客户端服务器“套接字已关闭”

问题描述:

我正在研究一个非常简单的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

在第61行的服务器上,当您第一次阅读时,您的客户端没有机会发送数据,因此它不会停在循环中,并向前移动以关闭第68行中的阅读器。

尝试创建一个类来处理在服务器上,这使得更容易想到的服务器做什么,像ClientHandler的将是一个不错的选择传入连接;)

有乐趣!