Java Reference
In-Depth Information
synchronized(clients)
{
for (Connection con: clients)
{
sb.append(con.clientName);
sb.append(" ");
}
broadcast("!"+sb.toString());
}
}
}
public static void main(String[] args)
{
try
{
System.out.println("ChatServer starting");
new ChatServer().runServer();
}
catch (IOException ioe)
{
System.err.println("unable to create server sock-
et");
}
}
}
constructor,a
void runServer()
method,aprivate
Connection
nestedclassthat
subclasses
Thread
, and a
main()
method that invokes this constructor followed by
runServer()
via method call chaining (see
Chapter 2
).
The constructor attempts to create a server socket; when successful, it creates an
array list that stores
Connection
objects representing incoming connection indica-
tions from chat clients.
The
runServer()
methodentersaninfiniteloopthatfirstinvokes
accept()
to
waitforaconnectionindicationandreturna
Socket
instanceforcommunicatingwith
theassociatedclient.Itthencreatesa
Connection
objectthat'slinkedtothe
Sock-