0

I'm working on a really simple Java Client / Server system (Just to get my feet wet with sockets). For some reason, I keep getting a "Socket is closed" error... here is my code..

Server File

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();
        }
    }
}

Client File

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;
        }
    }
}

I get the error in the client on line 41 and then a NullPointerException on line 46..

Thanks in advance for any help. I'm just trying to learn here.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
  • You might like to have a look at [this](http://stackoverflow.com/questions/14617331/java-socket-swingworker-running-but-no-message-received-or-transmitted/14617650#14617650) – MadProgrammer Feb 01 '13 at 03:52
  • Your exception handling is up the pole. Too many local catch blocks follow by live code that is reached unconditionally. Restructure all this. – user207421 Feb 01 '13 at 04:47

1 Answers1

0

in the server on line 61, when you do your first read, your client didn't had the oportunity to send data, so it don't stops in the loop and move forward to close the reader on line 68.

Try to create a class to handle incoming connections at the server, that makes easier to think about what to do in the server, something like ClientHandler would be a good choice ;)

have fun !

hamilton.lima
  • 1,902
  • 18
  • 29