2016-04-22 20 views
1

我试图建立一个被调用,运行使用。我试图让一个单独的线程的服务器运行如图所示插座的服务器摆动框架Swing应用程序,与ServerSocket的

Interface.java

public class Interface implements ActionListener{ 

User user; 

JFrame frame; 
JScrollPane scroll; 
JTextArea text; 
JButton b1,b2,b3,b4; 

ServerSocket ss; 
Socket socket; 
int port; 
ObjectInputStream in; 
ObjectOutputStream out; 
Packet packet; 

public Interface(User u) 
{  
    user=u; 

    port = DataBase1.getPortNumberOfUser(user.getUserName()); 

    frame=new JFrame(user.getUserName()+" - "+user.getEmail()); 

    text=new JTextArea("      Welcome "+user.getName()); 

    scroll = new JScrollPane(text); 

    b1=new JButton("UpLoad"); 
    b2=new JButton("GenerateKey"); 
    b3=new JButton("ShareKey"); 
    b4=new JButton("Exit"); 

    frame.setSize(500,400); 
    frame.setLayout(null); 
    frame.setVisible(true); 

    text.setBounds(5,5,250,350); 

    scroll.setBounds(5,5,250,350); 
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

    b1.setBounds(275,50,200,50); 
    b1.setActionCommand("upload"); 
    b1.addActionListener(this); 

    b2.setBounds(275,125,200,50); 
    b2.setActionCommand("generateKey"); 
    b2.addActionListener(this); 

    b3.setBounds(275,200,200,50); 

    b4.setBounds(275,275,200,50); 
    b4.setActionCommand("exit"); 
    b4.addActionListener(this); 


    frame.add(scroll); 
    frame.add(b1); 
    frame.add(b2); 
    frame.add(b3); 
    frame.add(b4); 

    try { 

     ss= new ServerSocket(port); 
     System.out.println("Socket Created !"); 
     while(true) 
     { 
      System.out.println("Waiting for Multi client .."); 
      socket = ss.accept(); 
      System.out.println("here"); 
      new MultiClientServerThread(socket,text).start(); 
     } 


    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
     try { 
      ss.close(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

MultiClientServerThread.java

public class MultiClientServerThread extends Thread { 

protected Socket socket; 
private ObjectInputStream in; 
private ObjectOutputStream out; 
private Packet packet; 
private JTextArea display; 

public MultiClientServerThread(Socket socket ,JTextArea display) { 
    in=null; 
    out=null; 
    packet=null; 
    this.socket=socket; 
    this.display=display; 
} 

public void logIn() 
{ 
    display.append("\n\n"+packet.getType()+" : "+packet.getContents()); 
} 

public void signUp() 
{ 
    display.append("\n\n"+packet.getType()+" : "+packet.getContents()); 
} 

public void run() { 
    try { 

     out = new ObjectOutputStream(socket.getOutputStream()); 
     in = new ObjectInputStream(socket.getInputStream()); 

     packet = (Packet)in.readObject(); 
     System.out.println("\n\nType : "+ packet.getType()+"\nKey : "+packet.getKey()+"\nContents : "+packet.getContents()); 
     if((packet.getType()).equals("LOGIN")) 
      logIn(); 
     else if((packet.getType()).equals("SIGNUP")) 
      signUp(); 
     out.writeObject("Received Your message client");   

    } catch(Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      in.close(); 
      out.close(); 
      socket.close(); 
      return; 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

}

但摆动的图形用户界面不加载,也就是我无法查看框架的内容。,任何人都可以请帮忙,在此先感谢...

+0

我知道,虽然Interface类中的循环看起来很可疑 – MadProgrammer

+1

在完成更新UI之前,您还可以使框架可见,也可以使用null布局对你没有好处 – MadProgrammer

回答

0

while(true)条件会让你在某个时刻内存不足。
删除它,你的框架应呈现asuming所有变量正确初始化。 (您可以在单独的线程中执行服务器轮询)