0

这是我在玩猜谜游戏时遇到的问题。基本上我想要做的是拥有一台服务器并让许多客户端连接到它。目前已经完成,我可以将客户端连接到服务器来玩游戏,猜数字游戏。问题是我希望每个单独的客户都能够玩游戏。目前该游戏正在服务器本身上进行。因此,虽然多个客户端可以加入,但每次客户端加入时游戏都会重新开始。当输入正确答案时,服务器会给客户他的分数。只是要清楚我正在运行服务器类,然后我正在运行客户端类。我希望能够在客户端类窗口而不是服务器窗口上玩游戏。这是我的代码,请您告诉我该怎么做。猜谜游戏源自java sun knock knock 教程。在这里找到http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html 谢谢。

客户端类

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

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

    Socket kkSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        kkSocket = new Socket("127.0.0.1", 4444);
        out = new PrintWriter(kkSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: taranis.");
        System.exit(1);
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    while ((fromServer = in.readLine()) != null) {
        System.out.println("Server: " + fromServer);
        if (fromServer.equals("Bye."))
            break;

        fromUser = stdIn.readLine();
    if (fromUser != null) {
            System.out.println("Client: " + fromUser);
            out.println(fromUser);
    }
    }

    out.close();
    in.close();
    stdIn.close();
    kkSocket.close();
}
}

服务器类

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

public class GServer {
public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    boolean listening = true;

    try {
        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444.");
        System.exit(-1);
    }
    System.err.println("Started KK server listening on port 4040");
    while (listening)
    new GThread(serverSocket.accept()).start();

    serverSocket.close();
}
}

协议类

import java.util.*;


public class GProtocol {
int guess = 0, number = new Random().nextInt(100) + 1;
int score = 10;
int guessmade = 0;
boolean gameRunning = true;
Scanner scan = new Scanner(System.in);


public String processInput(String theInput) {
    String theOutput = null;


    String ID;
    System.out.println("Please Enter your ID...");
    ID = scan.next( );

    System.out.println("Please guess the number between 1 and 100. You have 10 guesses. Your score is however many guesses you have left");

    while (guess != number)
    {

        try {


        if ((guess = Integer.parseInt(scan.nextLine())) != number) {

          System.out.println(guess < number ? "Higher..."  : "Lower...");
          score = score - 1; // here the score variable has one value taken away form it each time the user misses a guess
          guessmade = +1; // here the guess made variable is given +1 variable
        }
        else {
          System.out.println("Correct!");


        }

        }



      catch (NumberFormatException e) {
        System.out.println("Please enter valid numbers! '");

      }   






    }   


    theOutput = ID + "  your score is " + score ; // here the score is returned

    return theOutput;}}

线程类

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

public class GThread extends Thread {
private Socket socket = null;

public GThread(Socket socket) {
super("GMultiServerThread");
this.socket = socket;
}

public void run() {

try {
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                socket.getInputStream()));

    String inputLine, outputLine;
   GProtocol kkp = new GProtocol();
    outputLine = kkp.processInput(null);
    out.println(outputLine);

    while ((inputLine = in.readLine()) != null) {
    outputLine = kkp.processInput(inputLine);
    out.println(outputLine);
    if (outputLine.equals("Bye"))
        break;
    }
    out.close();
    in.close();
    socket.close();

} catch (IOException e) {
    e.printStackTrace();
}
}
}
4

2 回答 2

1

In Protocol class, you write Messages to System.out. The instance of the Protocol class is executed in the environment of the server, thus the output is printed to the server's output. To show the output in the client's console, you'll have to send the messages via the socket to the client and print it there.

于 2012-12-19T16:02:47.040 回答
0

首先,最好的做法是实现 Runnable而不是扩展 Thread。这不一定能解决你的问题。

现在,令人困惑的是您实际上想要实现的目标。您说您希望多个客户端玩游戏,但现在每个连接到服务器的客户端都开始一个新游戏。我认为这是正确的做法,但您似乎希望拥有一个游戏实例和多个客户端同时玩它。这是非常违反直觉的,但是如果您简单地创建GProtocol类的单个实例,将其传递给多个客户端并使用synchronize关键字来确保线程安全地访问其数据,您就可以实现它。

于 2012-12-19T16:04:35.910 回答