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");
}
}
}
Listing 9-2 's ChatServer class consists of private constant/nonconstant fields, a
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-
Search WWH ::




Custom Search