gpt4 book ai didi

java - 服务器启动后界面卡住

转载 作者:行者123 更新时间:2023-12-02 03:02:41 27 4
gpt4 key购买 nike

我正在创建一个客户端/服务器程序。客户端需要连接到连接到接口(interface)的服务器,并且每 5 分钟客户端发送一个字符串,该字符串将显示在服务器的界面中。这是服务器接口(interface)代码:

public class MainServer implements ActionListener {

public static JFrame f_principale;
JButton avvio_server;
JButton ferma_server;
public static JLabel risultato;
JButton richiesta;
Server server;

public static JTextArea ritorni;

public MainServer(){

UI_LOADER();

}

public void BUTTON_LOADER(){

avvio_server = new JButton("Avvia");
ferma_server = new JButton("Ferma");
richiesta = new JButton("Richiedi");

avvio_server.addActionListener(this);
ferma_server.addActionListener(this);
richiesta.addActionListener(this);

}

public void TEXT_LOADER(){

risultato = new JLabel();
risultato.setText("prova");
ritorni = new JTextArea();

}

public void WINDOW_LOADER(){

f_principale = new JFrame("Centro Meteorologica");
f_principale.setSize(800, 600);
f_principale.setVisible(true);
f_principale.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f_principale.setBackground(Color.gray);

f_principale.add(avvio_server);
f_principale.add(ferma_server);
f_principale.add(richiesta);
f_principale.add(ritorni);
f_principale.add(risultato);

avvio_server.setBounds(100, 400, 200, 100);
ferma_server.setBounds(500, 400, 200, 100);
richiesta.setBounds(350, 400, 100, 50);
ritorni.setBounds(100, 50, 600, 300);
risultato.setBounds(300, 530, 200, 50);



}

public void UI_LOADER(){

BUTTON_LOADER();
TEXT_LOADER();
WINDOW_LOADER();


}

public static void main(String[] args) {

MainServer m1 = new MainServer();


}

@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
if(button.getText() == "Avvia"){

System.out.print("Server avviato");
Thread server = new Server();
System.out.print("Server avviato");
server.run();
System.out.print("Server avviato");

}

if(button.getText() == "Ferma"){

try {
server.closeServer();
} catch (IOException e1) {

e1.printStackTrace();
}

}

if(button.getText() == "Richiedi"){


}

}

这是服务器代码:

public class Server extends Thread{

private ServerSocket serverSocket;
int clientPort;
ArrayList clients = new ArrayList();


public void run(){

Socket client;

try {

MainServer.ritorni.setText("Server Aperto");
serverSocket= new ServerSocket(4500);
System.out.println("Server Aperto");

} catch (IOException e) {
MainServer.risultato.setText("Errore nel aprire il server");
}


while(true){

client = null;

try {
client=serverSocket.accept();
MainServer.risultato.setText("Dispositivo collegato");
} catch (IOException e) {
MainServer.risultato.setText("Errore nel collegare il dispositivo");
}
clients.add(client);
Thread t=new Thread(new AscoltoDispositivo(client));
t.start();

}
}


public void closeServer() throws IOException{

serverSocket.close();

}

public void broadcastMessage(BufferedReader is, DataOutputStream os, Socket client) throws IOException{
for(Iterator all=clients.iterator();all.hasNext();){
Socket cl=(Socket)all.next();
sendMessage(cl);

}

}

private void sendMessage(Socket cl) throws IOException{
new DataOutputStream(cl.getOutputStream()).writeBytes("ASKRESPONSE");
}


}

class AscoltoDispositivo implements Runnable{
DataOutputStream os;
BufferedReader is;
Socket client;
static String tempReceived = null;

public AscoltoDispositivo(Socket client){

this.client=client;

try{

is= new BufferedReader(new InputStreamReader(client.getInputStream()));
os= new DataOutputStream (client.getOutputStream());

}catch(IOException e){
e.printStackTrace();
}
}

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

try {
tempReceived = is.readLine();
MainServer.ritorni.append(tempReceived);
} catch (IOException e) {
e.printStackTrace();
}

}
}
}

我唯一的问题是点击开始按钮后界面被卡住。

最佳答案

您的服务器在 UI 线程上运行:

if(button.getText() == "Avvia"){

System.out.print("Server avviato");
Thread server = new Server();
System.out.print("Server avviato");
server.start(); // <- not run()
System.out.print("Server avviato");
}

关于java - 服务器启动后界面卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com