java network programming
<p>I have two classes one is a server, one is a client, the client program accept an integer write to the server than create a game object then return a number to the client, if the number return is 0, the game stopped, otherwise keep continue to enter a integer. The following is the code, the problem is that, the server program only accept 1 times, than the client will throw an exception, i think there should a problem of the coding of the server program.</p>

<p>server.java</p>

<pre><code>import java.io.*;
import java.net.*;

class  ClientHandler extends Thread {

    Socket socket;
    Game g;

    public ClientHandler(Socket socket) {
        this.socket = socket;
        g = new Game(50);

    }

    public void run() {
        try {
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

            int x = dis.readInt();     


            int reply = this.g.guess(x);
            System.out.println(x);
            dos.writeInt(reply);

            //System.out.println(reply);
            //System.out.println(reply);

            socket.close();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

}
public class Server {
    public static void main(String arg[]) {
        try {
            ServerSocket ss = new ServerSocket(12345);

                        while(true) {
            Socket s = ss.accept();
            ClientHandler ch = new ClientHandler(s);
                        ch.start();

                        }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
</code></pre>

<p>client.java</p>

<pre><code>import java.util.Scanner;
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Client {

   public static void main(String args[]) {
      Socket socket = null;
      DataInputStream in = null;
      DataOutputStream out = null;

      Scanner console = new Scanner(System.in);
      int guess;
      int result;

      try {
         socket = new Socket("127.0.0.1", 12345);
         in = new DataInputStream(socket.getInputStream());
         out = new DataOutputStream(socket.getOutputStream());
         do {
            System.out.print("Guess a number: ");
            guess = console.nextInt();
            out.writeInt(guess);
            result = in.readInt();
            if (result == 0) {
               System.out.println("Correct!");
            } else if (result == -1) {
               System.out.println("Too small!");
            } else if (result == 1) {
               System.out.println("Too large!");
            } else {
               System.out.println("Too many attempts!");
            }
         } while (result != 0 &amp;&amp; result != -2);
      } catch (IOException ioe) {
         ioe.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            if (in != null) {
               in.close();
            }
            if (out != null) {
               out.close();
            }
            if (socket != null) {
               socket.close();
            }
         } catch (IOException ioe) {
            ioe.printStackTrace();
         }
      }
   }
}
</code></pre>
