简体   繁体   English

使用ServerSocket进行SWING应用程序

[英]SWING Application with ServerSocket

I am trying to build a swing frame that is invoked and runs the Server using socket .I have tried to make the server run on a separate thread as shown , 我正在尝试构建一个调用的swing框架并使用套接字运行服务器。我试图让服务器在一个单独的线程上运行,如图所示,

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

} }

but the swing GUI is not loading ,that is ,I am not able to view the contents of the frame ., Can anyone please help ,Thanks in advance ... 但摆动GUI没有加载,也就是说,我无法查看框架的内容。,任何人都可以请帮助,提前谢谢...

The while(true) condition will get you Out of Memory at some point. while(true)条件会在某些时候让你失去记忆。
Remove it and your frame should be rendered asuming all variables are correctly initialized. 删除它,你的框架应该被渲染,因为所有变量都已正确初始化。 (You can do the server polling in the separate thread) (您可以在单独的线程中进行服务器轮询)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM