gpt4 book ai didi

java - 保持 SocketServer 处于 Activity 状态,直到 Frame 关闭

转载 作者:行者123 更新时间:2023-12-01 14:39:24 24 4
gpt4 key购买 nike

我想要实现的是,通过我的程序,用户可以根据需要多次上传文件,当他们单击关闭按钮时,我想关闭或终止套接字连接,但是当我运行时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),连接将保持 Activity 状态,但即使当我关闭框架时,它仍然处于连接状态。

我在这里关闭了窗口的 @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();
}
}
}
}

关于java - 保持 SocketServer 处于 Activity 状态,直到 Frame 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com