使用ServerSocket进行SWING应用程序

时间:2016-04-22 10:43:01

标签: java swing sockets

我正在尝试构建一个调用的swing框架并使用套接字运行服务器。我试图让服务器在一个单独的线程上运行,如图所示,

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

}

但摆动GUI没有加载,也就是说,我无法查看框架的内容。,任何人都可以提供帮助,提前致谢...

1 个答案:

答案 0 :(得分:0)

while(true)条件会在某些时候让你失去记忆 删除它,你的框架应该被渲染,因为所有变量都已正确初始化。 (您可以在单独的线程中进行服务器轮询)