[英]Keep the SocketServer alive until the Frame is close
我要實現的目標是,通過我的程序,用戶可以根據需要多次上傳文件,並且當他們單擊關閉按鈕時,我想關閉或終止套接字連接,但是當我再次運行GUI時即使我還沒有開始,連接仍然存在。
while (true) { // >>>>>>>HERE IS MY PROBLEM<<<<<<<
try{
connectionSocket = welcomeSocket.accept();
inFromClient = new BufferedReader(new InputStreamReader(
connectionSocket.getInputStream()));
outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
//Do something here.....
}catch(IOException e){ }
}
因此,如果保留while(true),連接將保持活動狀態,但是即使關閉框架時,它仍在連接。
我在這里關閉了窗口的@Override方法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent b) {
if(socket != null){
try {
socket = new Socket(hostIP, port);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
if (socket != null) {
socket.close();
socket = null;
}
}
catch (IOException e) { socket = null; }
ftoc.dispose();
}
});
您有幾種選擇...
在套接字循環中創建一個“轉義”標志,該標志可以從外部觸發(例如,為false)。 將WindowListener
添加到框架中,並在windowClosing
翻轉標志並中斷線程。 這將允許循環終止。
您也可以嘗試在守護程序線程中設置服務器套接字,但是我懷疑這將無法正常工作,因為服務器套接字的阻塞操作可能不會在意。
更新了示例
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestSocket {
private SocketThread socketThread;
public static void main(String[] args) {
new TestSocket();
}
public TestSocket() {
socketThread = new SocketThread();
socketThread.start();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new JLabel("Close me if you dare"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
socketThread.stopThatSocket();
}
});
}
});
}
public class SocketThread extends Thread {
private volatile boolean flagToSetWhenYouWantToStop = true;
private ServerSocket socket = null;
public SocketThread() {
setName("Socket");
setDaemon(true);
}
public void stopThatSocket() {
flagToSetWhenYouWantToStop = false;
if (socket != null) {
try {
socket.close();
} catch (IOException ex) {
}
}
interrupt();
try {
join();
} catch (InterruptedException ex) {
}
}
@Override
public void run() {
try {
socket = new ServerSocket(1234);
while (flagToSetWhenYouWantToStop) {
Socket accept = socket.accept();
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
更新了簡單的套接字命令示例
這個示例基本通過套接字進行通信,告訴服務器應該關閉...
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.SocketFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestSocket {
private SocketThread socketThread;
public static void main(String[] args) {
new TestSocket();
}
public TestSocket() {
socketThread = new SocketThread();
socketThread.start();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new JLabel("Close me if you dare"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// socketThread.stopThatSocket();
Socket socket = null;
try {
socket = SocketFactory.getDefault().createSocket("localhost", 1234);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Out-Cmd = STOP");
bw.write("stop");
bw.newLine();
} finally {
try {
bw.close();
} catch (Exception exp) {
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception exp) {
}
}
}
});
}
});
}
public class SocketThread extends Thread {
private volatile boolean flagToSetWhenYouWantToStop = true;
private ServerSocket socket = null;
public SocketThread() {
setName("Socket");
setDaemon(true);
}
public void stopThatSocket() {
flagToSetWhenYouWantToStop = false;
if (socket != null) {
try {
socket.close();
} catch (IOException ex) {
}
}
interrupt();
try {
join();
} catch (InterruptedException ex) {
}
}
@Override
public void run() {
try {
socket = new ServerSocket(1234);
while (flagToSetWhenYouWantToStop) {
Socket accept = socket.accept();
/**
* Normally I would have a command processor take care of this,
* read in the command and then terminate the server thread by
* calling stopThatSocket...
*/
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
String cmd = br.readLine();
System.out.println("In-Cmd = " + cmd);
if (cmd.equalsIgnoreCase("stop")) {
stopThatSocket();
}
} finally {
try {
br.close();
} catch (Exception e) {
}
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.