为什么在尝试写入输出流时套接字会关闭?
堆栈跟踪:
Exception in thread "main" java.lang.RuntimeException: java.net.SocketException: Socket is closed
at chatx.server.handler.ExceptionHandler.handle(ExceptionHandler.java:14)
at chatx.server.handler.ExceptionHandler.handle(ExceptionHandler.java:6)
at chatx.server.Client.authenticate(Client.java:75)
at chatx.server.handler.SocketHandler.handle(SocketHandler.java:23)
at chatx.server.handler.SocketHandler.handle(SocketHandler.java:12)
at chatx.server.Server.listen(Server.java:117)
at chatx.server.Server.start(Server.java:61)
at chatx.server.Main.main(Main.java:30)
Caused by: java.net.SocketException: Socket is closed
at java.base/java.net.Socket.getInputStream(Socket.java:905)
at chatx.server.Client.authenticate(Client.java:65)
... 5 more
预身份验证阶段:
Client client = (Client) Optional.ofNullable(clientManager.getClient(socket.getInetAddress())).orElse(clientManager.addClient(new Client(socket)));
client.sendPacket(new PacketAuthentication(client.getUsername(), client.getPassword().toCharArray()));
if (client.authenticate()) {
System.out.printf("[Address: %s] Authenticated as %s.\n", client.getAddress(), client.getName());
client.start();
} else {
client.interrupt();
clientManager.removeClient(client);
System.out.printf("[Address: %s] Failed to authenticate.\n", client.getAddress());
}
身份验证阶段:
try {
JsonObject o = new GsonBuilder().create().fromJson(new String(socket.getInputStream().readAllBytes()), JsonObject.class);
if (o.has("packet_id") && o.get("packet_id").getAsInt() == PacketAuthentication.ID) {
JsonObject data = o.getAsJsonObject("data");
String username = data.get("username").getAsString();
String password = data.get("password").getAsString();
return this.username.equals(username) && this.password.equals(password);
}
} catch (IOException ex) {
Server.getInstance().getExceptionHandler().handle(ex);
}
客户端身份验证阶段:
IClient client = new Client(new Socket("127.0.0.1", 8888));
client.sendPacket(new PacketAuthentication("Dummy", "Dummy123".toCharArray()));
调用socket.getInputStream()
时,身份验证阶段会发生异常。
想法是让客户端在登录时发送身份验证数据包,并在服务器端验证信息。
供参考,我在github页面上有该项目: https://github.com/Azoraqua/ChatX/tree/development
编辑: 我可能已经发现了问题。 写入@MousaHalaseh
时,我在写OutputStream时使用了try-with-resources,但我只是意识到try-with-resources使OutputStream关闭。但是现在我又收到了关于“连接重置”的另一个例外:
Exception in thread "main" java.lang.RuntimeException: java.net.SocketException: Connection reset
at chatx.server.handler.ExceptionHandler.handle(ExceptionHandler.java:14)
at chatx.server.handler.ExceptionHandler.handle(ExceptionHandler.java:6)
at chatx.server.Client.authenticate(Client.java:86)
at chatx.server.handler.SocketHandler.handle(SocketHandler.java:23)
at chatx.server.handler.SocketHandler.handle(SocketHandler.java:12)
at chatx.server.Server.listen(Server.java:117)
at chatx.server.Server.start(Server.java:61)
at chatx.server.Main.main(Main.java:28)
Caused by: java.net.SocketException: Connection reset
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.base/java.io.InputStream.readAllBytes(InputStream.java:238)
at chatx.server.Client.authenticate(Client.java:76)
... 5 more
修改2: 通过关闭输出流的一端,解决了Edit 1中的问题。
答案 0 :(得分:2)
错误非常明显Socket is closed
,可能有助于解决此异常的事情:
socket.close();
。关闭返回的InputStream将关闭关联的套接字。
关闭返回的OutputStream将关闭关联的套接字。
编辑:
java.net.SocketException: Connection reset
通常意味着流的一端是封闭的,您说自己是
通过关闭输出流的一端修复了Edit 1中的问题。
保持输出流打开,您可能需要添加:
socket.setKeepAlive(true);
希望事情对您有用。