使用ThreadPool的Java Web服务器没有响应

时间:2017-04-04 11:36:29

标签: java multithreading webserver threadpool

我正在创建一个java Web服务器,它是测验游戏的一部分。出于测试目的,我已经包含了一个计数器。服务器有一个处理线程的FixedThreadPool。

服务器有一个Bufferedreader,它将从数据库和Printstream收集数据,我将用它将输出写入客户端。现在,Printstream的意图是打印出计数器的结果。但是当我运行服务器没有任何反应时,printStreamn没有按预期工作。终端中没有输出,客户端没有从服务器接收任何内容。怎么了?

服务器

 import java.io.*;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;


public class QuizServer {
private static final int serverPort = 9001;
public static void main(String[] args) throws Exception {

    ExecutorService theExecutor = Executors.newFixedThreadPool(4);
    System.out.println("The chat server is running.");
    ServerSocket quizSocket = new ServerSocket(serverPort);

    while (true) {  //Skriver ut While True

        Socket connection = quizSocket.accept();
        theExecutor.execute(new Broadcaster(connection));
         }
     }
 }

 class Broadcaster extends Thread {
     private BufferedReader read;
     private PrintStream write;
     private Socket broadcastSocket;

    static int counter = 0;

    public Broadcaster(Socket broadcastSocket) throws IOException{
    read = new BufferedReader(new 
    InputStreamReader(broadcastSocket.getInputStream()));
    write = new PrintStream(broadcastSocket.getOutputStream()); 

     }
     @Override
     public void run() {
        while (true) {

             try {
                 Thread.sleep(1000);
                 counter += 1;

                 write.println("Räknare: " + counter);

             } catch (InterruptedException e) {
                 System.out.println("Sleep exception: " + e);
             }
         }
    }
 }

调试

 Connected to the target VM, address: '127.0.0.1:1925', transport: 'socket'
 The chat server is running.
 Exception in thread "main" java.net.BindException: Address already in use: 
 JVM_Bind
 at java.net.DualStackPlainSocketImpl.bind0(Native Method)
 at java.net.DualStackPlainSocketImpl.socketBind
 (DualStackPlainSocketImpl.java:106)
 at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
 at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
 at java.net.ServerSocket.bind(ServerSocket.java:375)
 at java.net.ServerSocket.<init>(ServerSocket.java:237)
 at java.net.ServerSocket.<init>(ServerSocket.java:128)
 at QuizServer.main(QuizServer.java:19)
 Disconnected from the target VM, address: '127.0.0.1:1925', transport: 
 'socket'

 Process finished with exit code 1

客户端

public class Client implements Runnable {

    @FXML
    private Button bQuit = new Button();
    @FXML
    private TextArea Out = new TextArea();
    @FXML
    private TextField Input = new TextField();
    @FXML
    private TextField IP = new TextField();
    @FXML
    private TextField aliasField = new TextField();

     String alias;

     String ServerAddress = "localhost";
     String userAnswer;
     BufferedReader reader;
     PrintWriter writer;

     @FXML
     public void initialize() {
     bQuit.setOnAction(event -> Platform.exit());

     aliasField.setOnAction(a -> {

        alias = aliasField.getText();

        Input.setOnAction(b -> {
            userAnswer = Input.getText();
         });
    });
    aliasField.setPromptText("Alias");
    Input.setPromptText("And your answer is:");
          }

        @Override
public void run() {

    try {

        Socket clientSocket = new Socket(ServerAddress, 9001);
        reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        writer = new PrintWriter(clientSocket.getOutputStream(),true);   //Koppla user input hit.
       Scanner newObject= new Scanner(reader);

       while(newObject.hasNextLine())
        Out.setText(newObject.nextLine());
        //
       } catch (IOException e) {
      }
    }
  }

1 个答案:

答案 0 :(得分:1)

很可能客户端错了。

    reader = new BufferedReader(new 
    InputStreamReader(clientSocket.getInputStream()));
    writer = new PrintWriter(clientSocket.getOutputStream(),true);   
    Out.setText(String.valueOf(writer));
  • 您尝试从Out.text设置clientSocket.getOutputStream() 但是...... clientSocket.getOutputStream()是服务器的流,而不是服务器的流。

  • 您希望从String.valueOf(writer)获得什么?

在客户端上您需要读取服务器从输入读取器发送给您的字符串并将其设置为Out.text