私は本当に単純な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 が発生します。
助けてくれてありがとう。私はただここで学ぼうとしています。