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

时间:2013-02-01 03:28:58

标签: java sockets client port

我正在研究一个非常简单的Java客户端/服务器系统(只是为了让我的插座受到影响)。出于某种原因,我不断收到“Socket is closed”错误......这是我的代码..

服务器文件

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错误。

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

1 个答案:

答案 0 :(得分:0)

在第61行的服务器上,当你第一次阅读时,你的客户端没有机会发送数据,所以它不会在循环中停止并继续前进以在第68行关闭阅读器。 / p>

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

玩得开心!

相关问题