SAbbasizadeh
SAbbasizadeh

Reputation: 730

not complete connection in a simple server/socket program

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!

Upvotes: 0

Views: 169

Answers (1)

Gray
Gray

Reputation: 116918

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

Upvotes: 1

Related Questions