Java Reference
In-Depth Information
}
else if
(command.equals(Const.QUIT))
break;
else
response
#
Const.ERROR
!
" Unknown command";
}
catch
(Exception e){
response
#
Const.ERROR
!
" "
!
e.getMessage();
}
System.out.println("<"
!
response);
out.write((response).getBytes());
out.flush();
}
}
catch
(Exception ex)
{ } // do nothing, just jump out of the loop
}
}
The class
StubFactory
waits for a connection from a terminal, creates a
new
ServerStub
object, and assigns it to serve the new socket. It is a runnable
class; its
run()
method contains an infinite loop that accepts connections.
package
Market;
import
java.net.ServerSocket;
import
java.net.Socket;
public class
StubFactory
implements
Runnable {
private
Market market;
public
StubFactory(Market market){
this.market
#
market;
}
// this thread accepts connections from the counter
// terminals and for each connection creates a market
// stub to handle it
public void
run(){
try
{
ServerSocket ss
#
new
ServerSocket(Const.SERVER_PORT);
while
(
true
){
Socket sk
#
ss.accept();
MarketStub stub
#
new MarketStub(sk,market);
(
new
Thread(stub)).start();
}
}
catch
(Exception ex){ ex.printStackTrace() }
}
}
14.6.4
Test
The test of this prototype is the same as the previous one because no function-
ality was added. Only the internal communication mechanism has changed.