home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 4.5 KB | 169 lines |
- import java.io.*;
- import java.net.*;
- import java.awt.*;
-
- /**
- * NumberServer -- The server for a number guessing application.
- *
- * To run as a server, name the server as the first command line
- * argument:
- * java NumberServer bluehorse.com
- * or
- * java NumberServer local // to run locally
- */
-
- public class NumberServer {
- private int port = 5001; // The default port.
- private boolean listening = true;
-
- public static void main(String args[]) {
- new NumberServer().server();
- }
-
- // As a server, we create a server socket bound to the specified
- // port, wait for a connection, and then spawn a thread to
- // read data coming in over the network via the socket.
-
- private void server() {
- ServerSocket serverSock = null;
-
- try {
- serverSock = new ServerSocket(port, 50);
- } catch (IOException x) {
- System.out.println(x.getMessage() +
- ": Failed to create server socket.");
- System.exit(1);
- }
-
- while (listening) {
- try {
-
- Socket sock = serverSock.accept();
- new HandleGuesses(sock).start();
-
- } catch (IOException x) {
- System.out.println(x.getMessage() +
- ": Failed to connect to client.");
- System.exit(1);
- }
- }
-
- // At this point, we don't need the ServerSocket anymore.
- if (serverSock != null) {
- try {
- serverSock.close();
- } catch (IOException x) {
- }
- }
-
- }
-
- }
-
-
- /*
- * HandleGuesses communicates with the client.
- */
- class HandleGuesses extends Thread {
- private Socket sock;
- private DataInputStream remoteIn;
- private DataOutputStream remoteOut;
- private boolean listening = true;
- private int num = (int)(Math.random() * 10);
-
- public HandleGuesses(Socket sock) throws IOException {
- this.sock = sock;
- remoteIn = new DataInputStream(sock.getInputStream());
- remoteOut = new DataOutputStream(sock.getOutputStream());
- }
-
- public synchronized void run() {
- String s;
- String op;
- String guessString;
- int guessInt;
- try {
-
- while (listening) {
- s = remoteIn.readUTF();
- op = s.substring(0, 1);
- guessString = s.substring(1, 2);
- guessInt = new Integer(guessString).intValue();
-
- if (op.equals(">"))
- handleGreaterThan(guessInt);
- else if (op.equals("<"))
- handleLessThan(guessInt);
- else
- handleEquals(guessInt);
- }
-
- } catch (NumberFormatException x) {
- System.out.println(x.getMessage() +
- ": Protocol problem: expected a number.");
-
- } catch (IOException x) {
- System.out.println(x.getMessage() +
- ": Connection to peer lost.");
-
- } finally {
- try {
- if (remoteIn != null) {
- remoteIn.close();
- remoteIn = null;
- }
-
- if (remoteOut != null) {
- remoteOut.close();
- remoteOut = null;
- }
-
- if (sock != null) {
- sock.close();
- sock = null;
- }
-
- } catch (IOException x) {
- }
- }
- }
-
- private void handleGreaterThan(int i) throws IOException {
- if (num > i)
- remoteOut.writeUTF("true");
- else
- remoteOut.writeUTF("false");
- }
-
- private void handleLessThan(int i) throws IOException{
- if (num < i)
- remoteOut.writeUTF("true");
- else
- remoteOut.writeUTF("false");
- }
-
- private void handleEquals(int i) throws IOException {
- if (i == num) {
- remoteOut.writeUTF("You guessed it!");
- listening = false;
- }
-
- else
- remoteOut.writeUTF("false");
- }
-
- protected void finalize() throws Throwable {
- try {
- if (remoteIn != null)
- remoteIn.close();
- if (remoteOut != null)
- remoteOut.close();
- if (sock != null)
- sock.close();
- } catch (IOException x) {
- }
- super.finalize();
- }
-
- }
-