-1

This is quite a simple Java client/server application. Here I try to synchronize the accesses to the ArrayList. However, an exception is thrown. And I think I have protected everything I should. Can anyone help explain what's wrong with this code? Anything more to pay attention to?

public class ChatServerSynchronized {

    public static void main(String[] args) {
        new ChatServerSynchronized().go();
    }

    private ArrayList<ClientHandler> clients;
    private int numOfClients;
    public ChatServerSynchronized() {
        clients = new ArrayList<ClientHandler>();
        numOfClients = 0;
    }
    public void go() {
        try {
            ServerSocket ss = new ServerSocket(10000);
            System.out.println("Listening at 10000");
            while(true) {
                Socket s = ss.accept();
                int thisClient = (++numOfClients); 
                ClientHandler client = new ClientHandler(thisClient, s);
                System.out.println("New connection: "+thisClient);
                addClient(client);

                new Thread(client).start();
            }
        } catch (IOException e) {
        }
    }
    public synchronized void addClient(ClientHandler client) {
        clients.add(client);
    }
    public synchronized void removeClient(ClientHandler client) {
        clients.remove(client);
    }
    public synchronized void broadcast(String message) {
        for(ClientHandler client: clients) {
            client.send(message);
        }
    }

    private class ClientHandler implements Runnable {
        private BufferedReader in;
        private PrintWriter out;
        private int id;
        public ClientHandler(int clientID, Socket s) throws IOException {
            id = clientID;
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(s.getOutputStream());
        }
        public void run() {
            try {
                while(true) {
                    String message = in.readLine();
                    if(message == null)
                        break;
                    System.out.println(id+": Message '"+ message + "' received");
                    broadcast(id+": "+message);
                }
            } catch (IOException e) {
                //System.out.println(e);
                //This is where the exception is thrown
            } finally {
                System.out.println("Closed: "+id);
                removeClient(this);
            }
        }
        public void send(String message) {
            out.println(message);
            out.flush();
        }
    }

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
HuangJie
  • 1,488
  • 1
  • 16
  • 33

1 Answers1

-1

this is probably because u close the connection on the client and you save the stream on the server. Thus if you write to the closed socket (by the client closed) --> error.

Even if you don't write i would expect an error as soon as the clients stops the connection / aborts them. (someone should confirm this) set a Thread.sleep(5000) on youre client and then you see the error after 5 seconds probably. :)

Jeerze,

Simon

Simi
  • 174
  • 1
  • 7