简体   繁体   English

如何使套接字保持活动状态直到窗口关闭Java

[英]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) 我正在尝试实现该程序,该程序将允许用户从服务器(在同一台计算机上运行的client.java和server.java)上载/下载文件。

在此处输入图片说明

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 : 假设您正确设置了套接字 ,请使用WindowListener

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

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

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