SwingWorker 并从套接字检索数据
我有这样的代码
try {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(888);
} catch (IOException e) {}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {}
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
outputLine = "SRV:>"+inputLine+"<:VRS";
out.println(outputLine);
taWyjscie.append(outputLine+"\n");
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
} catch (IOException ex) {
taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
}
它可以工作,但是应用程序在客户端发送“再见”之前无法使用。
我希望能够通过 gui 从服务器向客户端发送消息,所以我与 SwingWorker 进行斗争,我得到了这个:
try {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(888);
} catch (IOException e) {}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {}
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
zadanie = new ServerTask(in);
} catch (IOException ex) {
taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
}
private class ServerTask extends SwingWorker<String, String> {
private BufferedReader iin;
public ServerTask(BufferedReader win) {
this.iin = win;
}
@Override
protected String doInBackground() throws Exception {
String input;
while ((input = iin.readLine()) != null) {
System.out.println(input);
publish(input);
if (isCancelled())
break;
}
return null;
}
@Override
protected void process(List<String> chunks) {
for(String el : chunks) {
textAreaOUT.append(el+"\n");
}
}
}
现在我无法从服务器向客户端发送消息,但来自客户端的消息不会显示在 textArea 中。我从早上就开始奋斗,我不知道摇摆工人是如何工作的……
PS。前面的 try ... catch 位于按钮操作执行函数中(如果有任何区别)
I have code like this
try {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(888);
} catch (IOException e) {}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {}
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
outputLine = "SRV:>"+inputLine+"<:VRS";
out.println(outputLine);
taWyjscie.append(outputLine+"\n");
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
} catch (IOException ex) {
taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
}
It works, but app is unusable until client sends "Bye.".
I want to be able to send messages to client from server by gui, so im fighting with SwingWorker, i got this:
try {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(888);
} catch (IOException e) {}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {}
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
zadanie = new ServerTask(in);
} catch (IOException ex) {
taWyjscie.append("Błąd I/O: "+ex.getLocalizedMessage() + "\n");
}
private class ServerTask extends SwingWorker<String, String> {
private BufferedReader iin;
public ServerTask(BufferedReader win) {
this.iin = win;
}
@Override
protected String doInBackground() throws Exception {
String input;
while ((input = iin.readLine()) != null) {
System.out.println(input);
publish(input);
if (isCancelled())
break;
}
return null;
}
@Override
protected void process(List<String> chunks) {
for(String el : chunks) {
textAreaOUT.append(el+"\n");
}
}
}
now i cant send message from server to client, but messages from client are not displayed in textArea. Im fighting with since morning and i cant get idea how swingworker is working...
PS. this try ... catch in front is in button actionperformed function (if it makes any differece)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从未在
SwingWorker
上调用execute
,这是您的第一个错误。从那里开始..You never call
execute
on yourSwingWorker
, thats your first bug. Start there..