FileDocCategorySizeDatePackage
TCPServer.javaAPI DocExample1266Sun Dec 07 10:45:00 GMT 2003javathreads.examples.ch12

TCPServer

public class TCPServer extends Object implements Runnable, Cloneable

Fields Summary
Thread
runner
ServerSocket
server
Socket
data
private boolean
done
Constructors Summary
Methods Summary
protected synchronized booleangetDone()

        return done;
    
public voidrun()

        if (server != null) {
            while (!getDone()) {
                try {
                    Socket datasocket = server.accept();
                    TCPServer newSocket = (TCPServer) clone();

                    newSocket.server = null;
                    newSocket.data = datasocket;
                    newSocket.runner =
                        new Thread(newSocket);
                    newSocket.runner.start();
                } catch (Exception e) {}
            }
        } else {
            run(data);
        }
    
public voidrun(java.net.Socket data)

    
public synchronized voidstartServer(int port)


           
        if (runner == null) {
            server = new ServerSocket(port);
            runner = new Thread(this);
            runner.start();
        }
    
public synchronized voidstopServer()

        done = true;
        runner.interrupt();