Java Reference
In-Depth Information
22
dispatcher.startDispatching(servSock, logger, protoFactory);
23
/* NOT REACHED */
24
}
25 }
ThreadMain.java
1. Application setup and parameter parsing: lines 0-13
2. Create server socket and logger: lines 15-16
3. Instantiate a protocol factory: lines 17-18
The protocol name is passed as the second parameter. We adopt the naming convention
of <ProtocolName>ProtocolFactory for the class name of the factory for the protocol
name <ProtocolName> . For example, if the second parameter is “Echo,” the corresponding
protocol factory is EchoProtocolFactory . The static method Class.forName() takes the
name of a class and returns a Class object. The newInstance() method of Class creates
a new instance of the class using the parameterless constructor. protoFactory refers to
this new instance of the specified protocol factory.
4. Instantiate a dispatcher: lines 19-20
The dispatcher name is passed as the third parameter. We adopt the naming convention
of <DispatcherType>Dispatcher for the class name of the dispatcher of type <Dispatcher−
Type> . For example, if the third parameter is “ThreadPer,” the corresponding dispatcher
is ThreadPerDispatcher . dispatcher refers to the new instance of the specified dispatcher.
5. Start dispatching clients: line 22
ThreadMain.java makes it easy to use other protocols and dispatchers. The topic's Web
site contains some additional examples. For example, see TimeProtocolFactory.java for an
implementation of the time protocol where clients can get the server time by simply connecting
to the server on the time port.
See GUIThreadMain.java on the topic's Web site for an example of server integration with
a GUI. This application lists the currently connected client. You will need a protocol-specific
GUI implementation (see GUIEchoProtocolFactory.java ). 1 The parameters to this application
are the same as for ThreadMain . For this application to work, you must specify the GUI version
of the protocol factory on the command line (e.g., GUIEcho instead of Echo ).
4.2
Nonblocking I/O
Socket I/O calls may block for several reasons. Data input methods read() and receive() block
if data is not available. A write() on a TCP socket may block if there is not sucient space to
buffer the transmitted data. The accept() method of ServerSocket and the Socket constructor
1 Clearly, more decomposition is possible, but it is beyond the scope of this topic.
Search WWH ::




Custom Search