从服务器套接字获取实时数据并在javafx中显示

您好,我需要从我创建的服务器套接字中获取一些数据,并将其显示在javafx应用程序中 FX应用程序,即显示屏每250毫秒刷新一次数据,服务器每2秒发送一次数据

我的代码/计划主要包括3部分 1.服务器生成数据,每2秒将其发送到端口 2. Clint代码从服务器获取数据并更新其全局变量 3.每250ms Schedule Scheduler执行器以clint形式访问全局变量并更新文本字段 //可悲的是这似乎不起作用 我总是启动服务器然后clint然后运行该应用程序

所以我写的代码如下

服务器代码

package minimalstructure;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
    Socket socket;
    try (ServerSocket serverSocket = new ServerSocket(5555)) {
        System.out.println("A");
        socket = serverSocket.accept();
        System.out.println("B");
        if(socket.isConnected())System.out.println("Connected");
        DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
        while (socket.isConnected()) {
            String T=DataStructureMaker();
            dout.writeUTF(T);
            System.out.println(T);
            TimeUnit.SECONDS.sleep(2);
            dout.flush();
        }
    }
    socket.close();
} 
public static String DataStructureMaker()
{
float RV_Phase=0,RI_Phase=0,RI_Grid=0,RV_Grid=0;String s="";
    DecimalFormat df = new DecimalFormat("#.00");
    s="";
    RV_Phase=Float.parseFloat(df.format((Math.random()*10)));
    s=s+Float.toString(RV_Phase)+"#";
    RI_Phase=Float.parseFloat(df.format((Math.random()*10)));
    s=s+Float.toString(RI_Phase)+"#";
    RI_Grid=Float.parseFloat(df.format((Math.random()*10)));
    s=s+Float.toString(RI_Grid)+"#";
    RV_Grid=Float.parseFloat(df.format((Math.random()*10)));
    s=s+Float.toString(RV_Grid)+"#";
    return s;
}

}

克林特代码是

package minimalstructure;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Clint {
public static String RV_Grid;
public static String RI_Grid;
public static String RI_Phase;
public static String RV_Phase;
public static void main(String[] args) throws IOException, InterruptedException {
    Socket s=new Socket("localhost",5555);
    String S;
    DataInputStream dIn=new DataInputStream(s.getInputStream());
    while (s.isConnected()) {            
        S=dIn.readUTF();
        setData(S);
    }
}
public static void setData(String S)  // Decryt data and set global values
{
    char[] A=S.toCharArray();
    int HC=0;
    String R="";
    if(A.length>2)
    for(char x:A)
    {
        if(x=='#')
        {
            switch(HC)
            {
                case 0:
                    HC++;
                    RV_Phase=R;
                    R="";
                    break;
                case 1:
                    HC++;
                    RI_Phase=R;
                    R="";
                    break;
                case 2:
                    HC++;
                    RI_Grid=R;
                    R="";
                    break;
                case 3:
                    HC++;
                    RV_Grid=R;
                    R="";
                    break;
            }
        }else{
            R=R+x;
        }
    }
}

}

最后是我的fxml控制器

package minimalstructure;import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {

@FXML
private TextField Text1;
@FXML
private TextField Text2;
@FXML
private TextField Text3;
@FXML
private TextField Text4;


static ScheduledExecutorService scheduledExecutorService;
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO

    scheduledExecutorService =         Executors.newSingleThreadScheduledExecutor();
    scheduledExecutorService.scheduleAtFixedRate(() -> {
        setData();

        }, 0, 250, TimeUnit.MILLISECONDS);
}    
public void setData()
{
    Text1.setText(Clint.RI_Grid);
    Text2.setText(Clint.RI_Phase);
    Text3.setText(Clint.RV_Grid);
    Text4.setText(Clint.RV_Phase);
}

}

上面的代码具有我的程序所需的所有无效数据加密,解密发送接收和显示