2017-02-14 91 views
0

即时创建客户端/服务器程序。客户端需要连接到连接到接口的服务器,每5分钟客户端发送一个字符串,该字符串将显示在服务器的接口中。 这是服务器接口代码:服务器启动后接口冻结

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(); 
       } 

      } 
     } 
} 

我唯一的问题是,经过我点击开始按钮的界面获得冻结。

+0

的',而(真)'是不是一个好的做法。另外,尝试在这个循环中添加一个定时器(例如'Thread.sleep()') –

+0

问题是,界面似乎以某种方式连接到服务器,但我将它设置为一个线程。 –

回答

2

你有你的服务器在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"); 
    }  
+0

它的工作,谢谢你:) –