Мулти-клиент сървър с отчитане броя и имената на клиентите

Клиент с име - протоколът за обмен започва с изпращането на името към сървъра работещ на порт 8085
import java.net.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class Client {
    private BufferedReader in;
    private PrintWriter out;
    private Socket socket;
    private String name;
    Client(Frame f){
        new Gui(f);
        init();
    }

    class Gui{
        TextArea serv;
        TextField cl;
        Gui (Frame f){
            f.setLayout(new BorderLayout());
            serv = new TextArea(20,10);
            serv.setEditable(false);
            cl = new TextField(30);
            f.add("Center",serv);
            f.add("South",cl);
            cl.addActionListener(new SrvL());
        }
        class SrvL implements ActionListener{
            public void actionPerformed(ActionEvent e){
                try{
                    String st=cl.getText();
                    send(st);
                    cl.setText("");
                    serv.append(receive()+"\n");
                }
                catch (Exception ex){
                    System.out.println("exception: "+ex);
                    System.out.println("closing...");
                    try{
                        socket.close();
                    }
                    catch (Exception expt){
                        System.out.println(expt);
                    }
                }
            }
        }
    }
    public void init(){
        try{
            String server = null;
            InetAddress addr = InetAddress.getByName(server);
            System.out.println("addr = " + addr);
            socket = new Socket(addr, 8085);
            System.out.println("socket = " + socket);
            BufferedReader sin = new BufferedReader(
                    new InputStreamReader(System.in));
            in =  new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            // Output is automatically flushed
            // by PrintWriter:
            out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())),true);
            System.out.print("Your name: ");
            name = sin.readLine();
            out.println(name);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("closing...");
            System.exit(4);
            try{
                socket.close();
            }
            catch(IOException ie){
                ie.printStackTrace();
                System.exit(3);
            }
        }

    }
    void send(String s){
        if(s.length()==0){
            out.println("END");
            System.out.println("closing...");
            try{
                socket.close();
            }
            catch (Exception expt){
                expt.printStackTrace();
            }
            System.exit(0);
        }
        else out.println(name+": "+s);

    }
    String receive(){
        String s="";
        try{
            s= in.readLine();
        }
        catch (Exception expt){
            expt.printStackTrace();
            System.exit(2);
        }
        return s;
    }

    public static void main(String[] args )throws IOException{
        Frame frame =new Frame();
        Client cl = new Client(frame);
        frame.setTitle(cl.name);
        frame.setSize(500,300);
        frame.setVisible(true);
    }
}

Сървър - въвежда  ресурс с имената  на закачените клиенти със синхронизирани методи

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;

class Names{
    private   ArrayList<String>  names;
    public Names(){
        names = new ArrayList<String>(10);
    }
    public synchronized void add(String name){
        names.add(name);
    }
    public synchronized void rm(String name){
        names.remove(name);
    }
    public synchronized String get(){
        String rez="\t"+names.size()+" clients connected: ";
        Iterator<String> itr = names.iterator();
        while(itr.hasNext()) {
            rez+=(String)itr.next()+"; ";
        }
        return rez;       
    }
}

class ServeOneClient extends Thread {
    private Names nm;
    private String name;
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    public ServeOneClient(Socket s, Names nm)  throws IOException {
        socket = s;
        this.nm = nm;
        in = new BufferedReader(
                new InputStreamReader(
                        socket.getInputStream()));
        // Enable auto-flush:
        out = new PrintWriter( new BufferedWriter(
                new OutputStreamWriter(
                        socket.getOutputStream()
                        )
                ),
                true);
        name = in.readLine();
        // If any of the above calls throw an
        // exception, the caller is responsible for
        // closing the socket. Otherwise the thread
        // will close it.
        start();    // Calls run()
    }
    public void run() {
        try {
            nm.add(name);
            System.out.println("connecting client "+name);
            while (true) {
                String str = in.readLine();
                if (str.equals("END")) break;
                System.out.println("Echoing: " + str+nm.get());
                out.println(str+nm.get());
            }
            System.out.println("closing client "+name);
        } catch (IOException e) {  }
        finally {
            nm.rm(name);
            try {
                socket.close();
            } catch(IOException e) {}
        }
    }
}

public class MtClSrv2 {
    static final int PORT = 8085;
    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        Names nm = new Names();
        System.out.println("Server Started");
        try {
            while(true) {
                // Blocks until a connection occurs:
                Socket socket = s.accept();
                try {
                    new ServeOneClient(socket,nm);
                } catch(IOException e) {
                    // If it fails, close the socket,
                    // otherwise the thread will close it:
                    socket.close();
                }
            }
        } finally {
            s.close();
        }
    }
}