gpt4 book ai didi

Java套接字swingWorker正在运行但没有接收或传输消息

转载 作者:行者123 更新时间:2023-12-02 14:11:34 25 4
gpt4 key购买 nike

几天前,我尝试创建一个服务器-客户端或客户端服务器作为实验来了解使用线程的套接字,但后来有人告诉我我应该使用 swingWorker。我做了一些研究如何使用并已在实践中实现它,但它仍然不起作用。即使我获得连接并使用了 .excute(),swingWorker 线程看起来也没有运行。如果你们能帮助找出我做错的地方,那就太好了。 SwingWorker 类位于 startSever() 和 startClient() 方法中。

    private void startServer() {
SwingWorker <Void, String> runningServer = new SwingWorker<Void, String>(){
protected Void doInBackground() {
try {
listeningSocket = new ServerSocket(port);
System.out.println("waiting for connection");
connection = listeningSocket.accept();
connected = true;
System.out.println("Connected");
String incomeMessage =null;
while(connected){
inStream = connection.getInputStream();
inDataStream = new DataInputStream(inStream);
if (myMessage !=null){
outStream = connection.getOutputStream();
outDataStream = new DataOutputStream(outStream);
outDataStream.writeUTF(myMessage);
}

if((incomeMessage = inDataStream.readUTF())!=null){
clientMessage = incomeMessage;
publish(clientMessage);
incomeMessage =null;
}
}
} catch (IOException e) {
clientMessage = "Connection Lost";
}
return null;
}
runningServer.execute();
}

最佳答案

这是一个非常基本的示例。

基本上,因为您的程序需要异步通信(也就是说,您需要能够同时从套接字读取数据并写入),所以您需要将每个流卸载到单独的线程。

这个例子的管理流程是不存在的。实际上,您应该有某种“连接”管理器,它能够干净地关闭输出和输入线程,这样,例如,当用户键入“再见”时,输出线程将能够告诉连接管理器:应终止连接。然后它会告诉输入线程停止读取任何新消息并终止...

客户端

public class Client {

public static void main(String[] args) {

try {
Socket master = new Socket("localhost", 8900);
new Thread(new InputHandler(master)).start();
new Thread(new OuputHandler(master)).start();
} catch (Exception ex) {
ex.printStackTrace();
}

}

public static class InputHandler implements Runnable {

private Socket socket;

public InputHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
boolean commune = true;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (commune) {
String text = reader.readLine();
System.out.println("\n<server> " + text);
if (text.toLowerCase().equals("bye")) {
commune = false;
}
}
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
}
try {
socket.close();
} catch (Exception e) {
}
}
}
}

public static class OuputHandler implements Runnable {

private Socket socket;

public OuputHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
boolean commune = true;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner scanner = new Scanner(System.in);
while (commune) {
System.out.print("> ");
String text = scanner.nextLine();
writer.write(text);
writer.newLine();
writer.flush();
if (text.equalsIgnoreCase("bye")) {
commune = false;
}
}
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
}
try {
socket.close();
} catch (Exception e) {
}
}
}
}
}

服务器

public class Server {

public static void main(String[] args) {

try {
ServerSocket master = new ServerSocket(8900);
Socket socket = master.accept();
new Thread(new InputHandler(socket)).start();
new Thread(new OuputHandler(socket)).start();
} catch (Exception ex) {
ex.printStackTrace();
}

}

public static class InputHandler implements Runnable {

private Socket socket;

public InputHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
boolean commune = true;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (commune) {
String text = reader.readLine();
System.out.println("\n<client> " + text);
if (text.toLowerCase().equals("bye")) {
commune = false;
}
}
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
}
try {
socket.close();
} catch (Exception e) {
}
}
}
}
public static class OuputHandler implements Runnable {

private Socket socket;

public OuputHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
boolean commune = true;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner scanner = new Scanner(System.in);
while (commune) {
System.out.print("> ");
String text = scanner.next();
writer.write(text);
writer.newLine();
writer.flush();
if (text.equalsIgnoreCase("bye")) {
commune = false;
}
}
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
}
try {
socket.close();
} catch (Exception e) {
}
}
}
}
}

更新(提示)

虽然我面前有你的源代码......

应该非常非常很少需要这样做textMessage.addKeyListener(this)

因为您使用的是 JTextField,所以您应该使用 ActionListener。造成这种情况的重要原因有很多,但对您来说,最主要的原因是“接受”操作依赖于外观和感觉。虽然大多数系统确实使用 Enter 作为“接受”操作,但这并不能保证。

看看How to Write a Action Listener了解更多信息

考虑到您正在尝试做的事情的总体复杂性,+1 表示整体良好的尝试!

关于Java套接字swingWorker正在运行但没有接收或传输消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617331/

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