Ali
Ali

Reputation: 10453

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)

enter image description here

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?

Upvotes: 0

Views: 586

Answers (1)

syb0rg
syb0rg

Reputation: 8247

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

Upvotes: 1

Related Questions