/* * Date: 04/04/04 * * Server.java - a simple socket server. * Daria Antonova + some code from John Neffenger */ import java.io.*; import java.net.*; /** *

This class accepts socket connections on the specified port and replies * to messages sent to it by clients. */ public class TestServer implements Runnable { private static final String USAGE = "Usage: Server [-p port] [-v]"; private static int TOTAL = 0; private static final int MAX = 100; private static int port = 6070; private static boolean verbose = false; // data structures for small database private static String[] names = new String[MAX]; // all student logins private static double[] halves = new double[MAX]; // all numbers private static int[][] scores = new int[MAX][MAX]; // all scores private Socket socket; private DataInputStream input; private PrintStream output; /** * Creates a connection from a socket. * @exception java.io.IOException when an error occurs creating the * connection. */ private TestServer(Socket socket) throws IOException { this.socket = socket; this.input = new DataInputStream(socket.getInputStream()); this.output = new PrintStream(socket.getOutputStream()); } /** * The body of the server test. */ public void run() { try { System.out.println("Echoing messages from client " + socket.getRemoteSocketAddress()); String line = "start"; String[] msg = new String[4]; String reply = "empty"; int temp; // exit with probability // echo data back to client while server receives data line = input.readLine(); while(line != null && line.compareTo("exit") != 0 && reply.compareTo("exit") != 0) { System.out.println("server got: " + line); msg = line.split(":",4); reply = "reply:error"; if(msg[0].compareTo("login") == 0) { reply = "replylogin:failed"; if(TOTAL < MAX) { names[TOTAL] = new String(msg[1]); reply = new String("replylogin:"+TOTAL); TOTAL++; } } if(msg[0].compareTo("request") == 0) { reply = "replyrequest:empty"; for(int i=0; i 0 && temp > 30) reply = "reply:"+names[(int)((Math.random()*100)%TOTAL)]; else { reply = "exit";} } if(msg[0].compareTo("guess") == 0) { reply = "replyguess:incorrect"; for(int i=0; i