Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6864365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:54:09+00:00 2026-05-27T02:54:09+00:00

I have two classes; one is a server, another one is a client, and

  • 0

I have two classes; one is a server, another one is a client, and the client program accepts an integer write to the server than creates a game object then return a number to the client, if the number returned is 0, the game stopped, otherwise keep continuing to enter a integer. The following is the code, the problem is that, the server program only accepts 1 times, then the client will throw an exception, and I think there should be a problem of the coding of the server program.

server.java

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();
        }
    }
}

client.java

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 && 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();
         }
      }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:54:09+00:00Added an answer on May 27, 2026 at 2:54 am

    May be you need to instantiate the socket and get streams (input and output) for each iteration in main method of Client class.

    do {
         socket = new Socket("127.0.0.1", 12345);
         in = new DataInputStream(socket.getInputStream());
         out = new DataOutputStream(socket.getOutputStream());
    
            System.out.print("Guess a number: ");
          ........
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes one of which inherits from the other. Im trying to
I have two classes, and want to include a static instance of one class
First I want to describe my situation briefly. I have two classes, one MainClass
I have got two classes, one a subclass of the other (say Animal and
I have the following two classes, one inherits from the other Class A{ void
Let's say I have two models, Classes and People. A Class might have one
I have two classes, a server and some clients. The server class make a
I have two classes. One is the entity class, the other serves as a
I have two PHP classes. One is for connecting to the database, building queries,
I have two classes - one that runs in the main thread and performs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.