Java Reference
In-Depth Information
in
#
null
;
out
#
null
;
}
}
The class
MarketStub
implements the other end of the communication
protocol. A new instance of this class is created every time a terminal con-
nects to the server. This class is run in a separate thread, the method
run()
consists of a long message loop that waits for a message, performs the
required operation and sends the reply containing the result.
package
Market;
import
java.net.Socket;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.util.StringTokenizer;
public class
MarketStub
implements
Runnable {
Socket socket;
Market market;
MarketStub(Socket sk, Market mk){
socket
#
sk;
market
#
mk;
}
// messages from the counter terminal are handled by
// the message loop that is executed in a
// separate thread
public
void run(){
try
{
InputStream in
#
socket.getInputStream();
OutputStream out
#
socket.getOutputStream();
byte [] buffer
#
new byte[1024];
long
customer
#
0;
long
transaction
#
0;
long
employee
#
0;
while
((
int
len
#
in.read(buffer)) !
#
-1){
String request
#
new String(buffer,0,len);
System.out.println(">"
!
request);
StringTokenizer parser
#
new
StringTokenizer(request);
String command
#
parser.nextToken();
String param
#
"";
String response;
if
(parser.hasMoreTokens())
param
#
parser.nextToken();
try
{