简体   繁体   中英

How to keep the socket alive until the window is close Java

I'm trying to implement this program that will allow the user to upload/download the file from the server (both the client.java and server.java running in the same machine)

在此处输入图片说明

The problem that I have is when they choose the file and click the button to upload it will not allow them to upload the second time because the socket is not alive anymore.

How do I make sure that I can get the socket to stay alive until the window is close?

Assuming you set up your socket properly , use a WindowListener :

// Listens for closing event
frame.addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent e)
    {
        // Do stuff
        socket.close(); // Make sure socket is visible here
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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