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 7775741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:42:23+00:00 2026-06-01T17:42:23+00:00

I have 2 classes which are GameServer and NetworkThread . The program create a

  • 0

I have 2 classes which are GameServer and NetworkThread. The program create a socket from somewhere else and then passes it to the NetworkThread‘s constructor
I just want to have 2 clients and these 2 interact with each other. First client’s id in the NetworkThread constructor is 1 and the other one’s is 2.
But when I start the 2nd client’s connection it will never enter to the NetworkThread class. Here is my code:

NetworkThread class:

public class NetworkThread implements Runnable, ActionListener {

    Socket newClient;
    String userName;
    int id;

    Scanner sc = null;
    PrintWriter pw = null;

    public NetworkThread(Socket newClient, String userName, int id) {
        this.userName = userName;
        this.newClient = newClient;
        this.id = id;

        try {
            pw = new PrintWriter(newClient.getOutputStream());
            sc = new Scanner(newClient.getInputStream());
        } catch (IOException e) {
        }

        ConstructFrame();
        if (id == 1)
            changeTurnAsAGuesser();
        else
            startConnectionForSecondPlayer();
    }

    @Override
    public void run() {
    }

    private void changeTurnAsAGuesser() {   
        String word;
        String passing;

        word = JOptionPane.showInputDialog(null, "Enter Word",
                "input" + userName, JOptionPane.QUESTION_MESSAGE).toUpperCase();
        passing = "~:";
            do {
                char x = sc.nextLine().charAt(0);
                // after some works with ~> passing
                pw.println(passing);
                pw.flush();
            } while (checkWord(word, passing) == false);

                 //checkWord method returns a boolean
        } catch (Exception e) {
        }
    }
    private void startConnectionForSecondPlayer() {
        new Thread() {
            public void run() {
                while (true) {
                    String taken = sc.nextLine();
                    jf.setTitle(taken);
                }
            }
        }.start();
    }
}

GameServer class:

public class GameServer {

    static int i = 0;
    ServerSocket gameServer = null;
    String userName;

    public GameServer(String username) {
        this.userName = username;
        new Thread() {
            public void run() {
                try {
                    gameServer = new ServerSocket(4444);
                } catch (IOException e) {
                }
                while (true) {
                    try {
                        i++;
                        Socket newClient = gameServer.accept();
                        System.out.println(i);
                        Thread th = new Thread(new NetworkThread(newClient,
                                userName, i));
                        th.start();
                    } catch (UnknownHostException e) {
                    } catch (IOException e) {
                    }
                }
            }
        }.start();
    }
}  

edit: I want to implement a Hangman game in which first client tell a word and the other one tries to guess that word but my main problem is that I can’t start a successful connection!

  • 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-06-01T17:42:25+00:00Added an answer on June 1, 2026 at 5:42 pm

    Ah I see it. You are doing all of the work in the constructor of your NetworkThread as opposed to the run() method. In the constructor, when id == 1, that calls changeTurnAsAGuesser in the same thread that did the accept. No other accept calls will be made until it returns.

    What you need to do is remove the if (id == 1) code from the constructor and move it to the run() method which is called by the background thread:

    public NetworkThread(Socket newClient, String userName, int id) {
        ...
        ConstructFrame();
        // don't do the interactive work in the constructor
    }
    
    @Override
    public void run() {
        // do it in the background thread
        if (id == 1)
            changeTurnAsAGuesser();
        else
            startConnectionForSecondPlayer();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes which get private properties set via the constructor. I would then
I have two classes which second inherit by first. then I have this: First[]
I have some classes which are parameterized via their constructor. I need to provide
I have two classes which both extend from SQL class like this: class SQL
I have classes which are inherited from abstract Packet( this class has abstract method
I have several Abstract Base Classes which act like interfaces as known from Java
I have classes which have automatic properties only like public customerName {get; set;}. They
I have four classes which share some arrangement of four properties. I have currently
I have some classes which extends a superclass, and in the JSP I want
I have various classes which all contain address details, i.e. AddressLine1, AddressLine2, AddressLine3, Suburb,

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.