0

我正在开发一个非常简单的 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 错误。

提前感谢您的帮助。我只是想在这里学习。

4

1 回答 1

0

在第 61 行的服务器中,当您第一次读取时,您的客户端没有机会发送数据,因此它不会在循环中停止并继续关闭第 68 行的读取器。

尝试创建一个类来处理服务器上的传入连接,这样可以更容易地考虑在服务器上做什么,像 ClientHandler 这样的东西会是一个不错的选择;)

玩得开心 !

于 2013-02-01T03:39:26.637 回答