更改客户端 - 服务器应用程序中的圆的颜色

时间:2016-03-02 11:59:42

标签: java multithreading sockets javafx server

我在使用套接字编程客户端服务器时遇到一些问题,我希望同时连接多个客户端。 问题是我有一个客户端GUI,我想在服务器发送信号时改变圆圈的颜色。

我在客户端窗口中使用JavaFX。

我遇到的问题是服务器运行连接(使用线程)并在完成后对圆圈进行所有更改,而我希望它是动态完成的。

这是我的clientserver-thread-code。它将参数发送到客户端拾取的套接字,并相应地显示颜色,然后在更改另一个圆的颜色之前休眠一段固定的时间。我还有一个服务器类,它使用给定的portnumber启动服务器并持续监听客户端。

 class ClientServer extends Thread
{
Socket connectSocket;
InetAddress clientAddr;
String parametersFromClient;
char[] params;


public ClientServer(Socket connectSocket)
{
    this.connectSocket = connectSocket;
    clientAddr = connectSocket.getInetAddress();
}

public void run()
{
    try (

           //writer to the connection socket
            PrintWriter output = new PrintWriter(connectSocket.getOutputStream(), true);

            // Stream reader from the connection socket
            BufferedReader input = new BufferedReader(new InputStreamReader(connectSocket.getInputStream()));
    )
    {
          int[] lights = MultipleClientsServer.lightvalues;
        while (((parametersFromClient = input.readLine()) != null) ) //FIKS!!!
        {
            if(parametersFromClient.equals("connect"))
            {
            System.out.println(parametersFromClient);
            output.println("red");
            try {TimeUnit.SECONDS.sleep(lights[0]);} catch (InterruptedException e) {}

            output.println("yellow");
            try {TimeUnit.SECONDS.sleep(lights[1]);} catch (InterruptedException e) {}

            output.println("green");
            try {TimeUnit.SECONDS.sleep(lights[2]);} catch (InterruptedException e) {}

            output.println("");
            }
        } 

        System.out.println(MultipleClientsServer.IPList.toString());
        MultipleClientsServer.removeFromList(clientAddr.toString());
        // close the connection socket
        connectSocket.close();
        //søke etter inetadress i linkedlist for å fjerne
        System.out.println(MultipleClientsServer.IPList.toString());


    } catch (IOException e)
    {
        System.out.println("Exception occurred when trying to communicate with the client " + clientAddr.getHostAddress());
        System.out.println(e.getMessage());
    }
}

在我的客户端代码中,我有一个连接方法,只要按下按钮就会启动:(红色,黄色和绿色是我想改变颜色的圆圈。)

public static Socket clientStart()
 {
   String hostName = "127.0.0.1"; // Default host, localhost
  int portNumber = 5555; // Default port to use

     try
    (
        // create TCP socket for the given hostName, remote port PortNumber
        Socket echoSocket = new Socket(hostName, portNumber);

    )
     {
         String s = "";
      System.out.println("Sender til ip: " + hostName + " port: " + portNumber);
      // Stream writer to the socket
       out = new PrintWriter(echoSocket.getOutputStream(), true);
      // Stream reader from the socket
       in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); 
       out.println("connect");

       while((s = in.readLine()) != null && !s.isEmpty())
       {
             switch (s)
             {
                 case "red":
                     //set circle to red.
                     red.setFill(Paint.valueOf("#ff1f1f"));
                     System.out.println("red");
                     break;
                 case "yellow":
                     //set circle to yellow
                     yellow.setFill(Paint.valueOf("#36bc0d"));
                     System.out.println("yellow");
                     break;
                 case "green":
                     //set circle to green
                     green.setFill(Paint.valueOf("#bdff1f"));
                     System.out.println("green");
                     break;
                 default:
                     break;
             }
       }

      return echoSocket;
     }
     catch (UnknownHostException e)
    {
        System.err.println("Don't know about host " + hostName);
        System.exit(1);

    } catch (IOException e)
    {
        System.err.println("Couldn't get I/O for the connection to " +  hostName);
        System.exit(1);
    }
     return null;
 }

问题是客户端打印出从服务器接收的参数,然后睡眠一段固定的时间,但它不会改变圆圈的颜色直到最后,并且所有这些都在同一时间。< / p>

有没有人有解决方案?非常感谢。

1 个答案:

答案 0 :(得分:1)

在后台线程中运行clientStart方法,并在Platform.runLater(...)中包含更改用户界面的调用:

while((s = in.readLine()) != null && !s.isEmpty()) {
    final String line = s ;
    Platform.runLater(() -> {
         switch (line)
         {
             case "red":
                 //set circle to red.
                 red.setFill(Paint.valueOf("#ff1f1f"));
                 System.out.println("red");
                 break;
             case "yellow":
                 //set circle to yellow
                 yellow.setFill(Paint.valueOf("#36bc0d"));
                 System.out.println("yellow");
                 break;
             case "green":
                 //set circle to green
                 green.setFill(Paint.valueOf("#bdff1f"));
                 System.out.println("green");
                 break;
             default:
                 break;
         }
    });
}

然后你需要用以下内容来呼叫clientStart()

Task<Socket> clientStartTask = new Task<Socket>() {
    @Override
    public Socket call() {
        return clientStart();
    }
});

clientStartTask.setOnSucceeded(e -> {
    Socket socket = clientStartTask.getValue();
    // do whatever you need with socket here...
});

Thread thread = new Thread(clientStartTask);
thread.setDaemon(true);
thread.start();