java - 客户端和服务器如何暂停以等待对方发送下一条消息?

标签 java multithreading

我正在构建一个 Java 客户端和服务器来玩猜我的数字游戏。服务器告诉客户端数字的范围,客户端输入猜测,如果客户端猜对了正确的数字,服务器将返回“再试一次”或该数字。服务器也需要是多线程的。

我已经完成了服务器发送范围的部分,但我无法让服务器和客户端在 Client.java 和 WorkerRunnable.java 中的注释指示的点处暂停。我需要服务器和客户端此时暂停,以便等待接收来自相应发送者的消息。

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

public class GameServer implements Runnable{

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

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

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

public void run() {

    synchronized(this) {
        this.runningThread = 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.");
                return;
            }
        }
        new Thread(new WorkerRunnable(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 WorkerRunnable implements Runnable{

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

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

    public WorkerRunnable(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.");

        }

    }

}

最佳答案

你的问题是你正在读行,但你没有写行。您需要在消息中添加行终止符。

这与您的标题或您在问题正文中要求的内容无关,无论如何,它们彼此不一致。读取将阻塞,直到数据到达。在这种情况下,readLine() 将阻塞,直到行终止符或流末尾到达。

关于java - 客户端和服务器如何暂停以等待对方发送下一条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379979/

相关文章:

java - 不同线程中的 DecimalFormat.format(double)

java.time.DateTimeException : Hijrah date out of range 异常

ruby-on-rails - 测量多线程环境中线程的执行时间

multithreading - Delphi - OTL - ThreadPool 和 Worker 线程之间的通信

c++ - C++中的多线程

multithreading - 如何使我的应用程序在多个内核上运行?

java - log4j 附加程序之间的依赖关系

java - 在一个时间间隔运行一个方法

c# - 多线程代码使 Rhino Mocks 导致死锁

java - if 语句中的多个 instanceof