خرید بک لینک

Vote count: 0

I am building a java client and server that plays a guess my number game. The server tells the client the range of numbers, the client enters a guess, and the server retus "Try Again" or the number if the client guessed the correct number. The server also needs to be multithreaded.

I have up to the part where the server sends the range but I caot get the server and the client to pause at the points indicated by the Idea in Client.java and in WorkerRuable.java. I need the server and client to pause at this point so that it will wait to receive the message from the corresponding sender.

import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;

public class GameServer implements Ruable{

    public int serverPort = 8080;
    public ServerSocket serverSocket = null;
    public boolean isStopped = false;
    public Thread ruingThread = null;

public GameServer() {
    this.serverPort = 8080;
}

public GameServer(int port) {
    this.serverPort = port;
}

public void run() {

    synchronized(this) {
        this.ruingThread = Thread.currentThread();
    }
    openServerSocket();
    while(!this.isStopped) {
        Socket clientSocket = null;
        try {
            clientSocket = this.serverSocket.accept();
        }catch (IOException e) {
            if(this.isStopped) {
                System.out.println("The server has been stopped.");
                retu;
            }
        }
        new Thread(new WorkerRuable(clientSocket, "Oi m8")).start();
    }
    System.out.println("The server has been stopped.");

}

    public synchronized void stop() {
        this.isStopped = true;
        try {
            this.serverSocket.close();
        }catch (IOException e) {
            System.out.println("Error closing server.");    
        }
    }

    private void openServerSocket() {
        try {
            this.serverSocket = new ServerSocket(this.serverPort);
        }catch (IOException e) {
            System.out.println("Error opening server.");
        }
    }

}


public class Server {

    public static void main(String [] args) {

        GameServer server = new GameServer(9000);
        new Thread(server).start();

        System.out.println("The Server has started.");

    }

}


import java.io.*;
import java.net.*;

public class Client {

   public static void main(String [] args) throws IOException{

       Socket clientSocket = new Socket("localhost", 9000);

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        String serverMessage;
        String clientMessage;

        serverMessage = inFromServer.readLine();
        System.out.println(serverMessage);

        System.out.print("Enter a number: ");
        String num = inFromUser.readLine();

        //I need for the client to wait here as well.

        System.out.println("ugh");

    }

} 


import java.net.*;
import java.io.*;
import java.util.*;

public class WorkerRuable implements Ruable{

    public Socket clientSocket = null;
    public String serverText = null;

    public int number = -1; 
    public int guessCount = 0;

    public WorkerRuable(Socket clientSocket, String serverText) {

        this.clientSocket = clientSocket;
        this.serverText = serverText;
        this.guessCount = 0;

    }

    public void run() {

        try {

            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            OutputStream output = clientSocket.getOutputStream();

            Random rand = new Random();
            number = rand.nextInt(10);

            output.write(("Guess an integer between 0 and 10." + number).getBytes());

            //I need for the server to wait here

            output.close();


        }catch (IOException e) {

            System.out.println("Something went wrong.");

        }

    }

}

asked 37 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 15 فروردين 1395 ساعت: 6:55

صفحه بندی