当我关闭JsonWriter
时,它是否也会关闭套接字?因为我不想那样。我希望服务器保持连接,以便我可以向它发送命令。就像我正在编码的那个(命令)是“getplayers”。客户端发送带有该文本(String)“getplayers”的对象。然后,服务器在命令之间切换,并执行客户端发送的任何命令。如果发送JsonWriter
的{{1}}在完成“getplayers”后关闭套接字,但这不是我想要的。
服务器代码:
playerList
客户端代码(我知道它不能像这样工作 - 读取它在textField中没有正确显示的数据,但有些东西正在关闭连接,这是我不想先发生的事情):
switch (command) {
case "getplayers":
qryFullPlayerTable = mySQLConnection.prepareStatement("SELECT pid,uuid,nickname,firstjoined,lastquit FROM PlayerList");
result = qryFullPlayerTable.executeQuery();
clientHandlerOut.writeObject("getplayers");
final JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(clientHandlerOut));
resultSetToJsonArrayStream(result, jsonWriter);
jsonWriter.close();
客户端检查TextField以获取命令,如果它是“getplayers”,它会向服务器发送“getplayers”。换句话说,服务器只是监听命令,并发送播放器数据。它是一个clientHandler,因此它应保持连接,直到客户端断开连接。
我得到的错误 - 在服务器上:
[16:04:56] [Thread-10 / WARN]:java.net.SocketException:Socket已关闭 [16:04:56] [Thread-10 / WARN]:at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118) [16:04:56] [Thread-10 / WARN]:at java.net.SocketOutputStream.write(SocketOutputStream.java:155) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream中的$ BlockDataOutputStream.drain(ObjectOutputStream.java:1877) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream中的$ BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577) [16:04:56] [Thread-10 / WARN]:at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351) [16:04:56] [Thread-10 / WARN]:at de.freakyonline.uconeplugin.UCOnePluginServer $ ClientHandler.run(UCOnePluginServer.java:112) [16:04:56] [Thread-10 / WARN]:at java.lang.Thread.run(Thread.java:745)
在客户端:
java.net.SocketException:socket关闭了 java.net.SocketInputStream.socketRead0(Native Method)at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)at java.net.SocketInputStream.read(SocketInputStream.java:170)at java.net.SocketInputStream.read(SocketInputStream.java:141)at java.net.SocketInputStream.read(SocketInputStream.java:223)at java.io.ObjectInputStream中的$ PeekInputStream.peek(ObjectInputStream.java:2321) 在 java.io.ObjectInputStream中的$ BlockDataInputStream.peek(ObjectInputStream.java:2614) 在 java.io.ObjectInputStream中的$ BlockDataInputStream.peekByte(ObjectInputStream.java:2624) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1321) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) 在de.freakyonline.ucone.Remote.run(Remote.java:53)at java.lang.Thread.run(Thread.java:745)
是否有可能阻止switch(command) {
case "getplayers":
/*while((obj2=in.readObject()) != null) {
currentFromServer = obj2.toString().toLowerCase();
if (currentFromServer.equalsIgnoreCase("done")) {
consoleOneTextArea.appendText("This is finished. Have fun!\n");
break;
}
consoleOneTextArea.appendText(currentFromServer + "\n");
*/JsonReader jsonReader = new JsonReader(new InputStreamReader(in, "UTF-8"));
try {
while(jsonReader.hasNext()){
JsonToken nextToken = jsonReader.peek();
System.out.println(nextToken);
if(JsonToken.BEGIN_OBJECT.equals(nextToken)){
jsonReader.beginObject();
} else if(JsonToken.NAME.equals(nextToken)){
String name = jsonReader.nextName();
switch(name) {
case "pid":
System.out.println("pid: " + jsonReader.nextInt());
default:
System.out.println("TODO: " + name);
}
}
jsonReader.close();
break;
关闭套接字?或者还有另一种方法来实现这一目标吗?
答案 0 :(得分:1)
是的,如果您close()
JsonWriter
,基础套接字也会关闭。
要避免套接字被关闭,只是不要关闭编写器,而是让它被垃圾收集。虽然您可能希望确保所有内容都是flush()
'd。
close()
的目的是刷新任何缓冲区并释放任何底层资源(输出流,文件句柄,套接字......),但由于你明确不希望这样,你不需要需要这样做。