1
All Javafx screens lock when the server is turned on.
I tried to use task, but you don’t seem to be getting ahead of yourself. The program works even with the locked screens but I would like to be able to access the interface while the server is connected.
I was part of the code based in this example.
Follow the control class:
public class FXMLTelaServidorController implements Initializable {
//variaveis usadas no controle do servidor
private static ServerSocket server;
private static ArrayList<BufferedWriter>clientes;
private String nome;
private Socket con;
private InputStream in;
private InputStreamReader inr;
private BufferedReader bfr;
//variaveis usadas na parte de visualizaçao
@FXML
private Pane painel;
@FXML
private Label labelLigarServidor;
@FXML
private Label labelIP;
@FXML
private Label labelPorta;
@FXML
private TextField campoTextoIP;
@FXML
private TextField campoTextoPorta;
@FXML
private Button botaoLigar;
@FXML
private Button botaoDesligar;
/**
* Método construtor(seria se o javafx aceitasse thread de boas) como nao aceitou transformei em um metodo normal
* @param con1
*/
public void FXMLTelaServidorController(Socket con1){
this.con = con1;
try {
in = con.getInputStream();
inr = new InputStreamReader(in);
bfr = new BufferedReader(inr);
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
void ligarServidor(ActionEvent event) {
try{
//Cria os objetos necessário para instânciar o servidor
campoTextoPorta.getText();
Object[] texts = {"porta do servidor", campoTextoPorta.getText()};
JOptionPane.showMessageDialog(null, texts);
server = new ServerSocket(Integer.parseInt(campoTextoPorta.getText()));
//clientes = new ArrayList<BufferedWriter>();
JOptionPane.showMessageDialog(null,"Servidor ativo na porta: "+
campoTextoPorta.getText());
System.out.println("Aguardando conexão...");
Socket con1 = server.accept();
System.out.println("Cliente conectado...");
//inicio da task
Task<String> servidor = new Task<String>() {
@Override
protected String call() throws Exception {
return "deu certo";
}
@Override
protected void succeeded() {
JOptionPane.showMessageDialog(null, this);
}
};
//fim da task
FXMLTelaServidorController(con1);
Thread t = new Thread(servidor);
t.setDaemon(true);
t.start();
Platform.runLater(t);
//codigo pego do run
try{
String msg;
OutputStream ou = con.getOutputStream();
Writer ouw = new OutputStreamWriter(ou);
BufferedWriter bfw = new BufferedWriter(ouw);
clientes = new ArrayList<BufferedWriter>();
clientes.add(bfw);
nome = msg = bfr.readLine();
while(!"Sair".equalsIgnoreCase(msg) && msg != null)
{
msg = bfr.readLine();
sendToAll(bfw, msg);
System.out.println(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
//fim do codigo pego do run
}catch (Exception e) {
e.printStackTrace();
}
}
@FXML
void desligarServidor(ActionEvent event) {
Thread.currentThread().interrupt();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
/***
* Método usado para enviar mensagem para todos os clients
* @param bwSaida do tipo BufferedWriter
* @param msg do tipo String
* @throws IOException
*/
public void sendToAll(BufferedWriter bwSaida, String msg) throws IOException{
BufferedWriter bwS;
for(BufferedWriter bw : clientes){
bwS = (BufferedWriter)bw;
if(!(bwSaida == bwS)){
bw.write(nome + " : " + msg+"\r\n");
bw.flush();
}
}
}
}
The methodology for working with javafx sockets is different, take a look at this link: https://blogs.oracle.com/jtc/update-to-javafx,-sockets-and-threading:-lessons-learned
– Gustavo Fragoso
Vlw Gustavo gave a read and after testing some things based on this example of the link I realized that the error was the way I used the Task and the runLater
– edsaon