Ignore EOFException too. try-catch + auto-closeable
This commit is contained in:
parent
7608e43669
commit
6729de5894
1 changed files with 5 additions and 11 deletions
|
|
@ -55,12 +55,11 @@ public class ServerThread extends Thread {
|
||||||
public void run() {
|
public void run() {
|
||||||
this.working.set(true);
|
this.working.set(true);
|
||||||
while (this.working.get() && !this.serverSocket.isClosed()) {
|
while (this.working.get() && !this.serverSocket.isClosed()) {
|
||||||
try {
|
try (final Socket socket = this.serverSocket.accept();
|
||||||
final Socket socket = this.serverSocket.accept();
|
final DataInputStream inputStream = new DataInputStream(socket.getInputStream());
|
||||||
|
final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream())) {
|
||||||
socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS);
|
socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS);
|
||||||
// System.out.println("[LegacyPing] Got new ping from " + socket.getRemoteSocketAddress());
|
|
||||||
final DataInputStream inputStream = new DataInputStream(socket.getInputStream());
|
|
||||||
final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
|
|
||||||
final int packetLength = inputStream.readByte() | inputStream.readByte() << 1; // idk why it doesn't a VarInt
|
final int packetLength = inputStream.readByte() | inputStream.readByte() << 1; // idk why it doesn't a VarInt
|
||||||
final int protocolVersion = this.readVarInt(inputStream);
|
final int protocolVersion = this.readVarInt(inputStream);
|
||||||
final String hostname = this.readUtf(inputStream);
|
final String hostname = this.readUtf(inputStream);
|
||||||
|
|
@ -76,15 +75,10 @@ public class ServerThread extends Thread {
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
|
|
||||||
inputStream.readShort(); // legacy data
|
inputStream.readShort(); // legacy data
|
||||||
|
|
||||||
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet length (9)
|
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet length (9)
|
||||||
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet id (1)
|
this.writeVarInt(outputStream, this.readVarInt(inputStream)); // Packet id (1)
|
||||||
outputStream.writeLong(inputStream.readLong()); // timestamp
|
outputStream.writeLong(inputStream.readLong()); // timestamp
|
||||||
|
} catch (SocketTimeoutException | EOFException ignore) {
|
||||||
inputStream.close();
|
|
||||||
outputStream.close();
|
|
||||||
socket.close();
|
|
||||||
} catch (SocketTimeoutException ignore) {
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
System.out.println("Exception caught: " + ex);
|
System.out.println("Exception caught: " + ex);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue