Compare commits

..

2 Commits

4 changed files with 18 additions and 13 deletions

View File

@ -34,7 +34,7 @@ public class Main {
if (args.length > 1) {
boolean isProxied = false;
if (args.length > 2)
isProxied = args[2].equals("on") ? true : false;
isProxied = args[2].equals("on");
try {
server = new CrabServer(Integer.parseInt(args[1]), isProxied);
} catch (NumberFormatException e) {
@ -42,15 +42,12 @@ public class Main {
return;
}
} else {
System.err.println("Now enough arguments.");
System.err.println("Not enough arguments.");
return;
}
server.run();
}
default -> {
System.err.println("Unknown argument");
return;
}
default -> System.err.println("Unknown argument");
}
}

View File

@ -6,6 +6,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.Scanner;
public class CrabServer implements Crab {
@ -92,8 +93,11 @@ public class CrabServer implements Crab {
isStopped = true;
try {
if (serverSocket != null) serverSocket.close();
getDb().close();
} catch (IOException e) {
System.err.println("An error occured while closing the socket: " + e.getMessage());
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
System.exit(0);
}

View File

@ -61,4 +61,8 @@ public class Database {
String logsString = s.toString();
return new Logs(logsString.isEmpty() ? 0 : logsString.getBytes().length, logsString);
}
public void close() throws SQLException {
connection.close();
}
}

View File

@ -47,7 +47,7 @@ public class ServerThread implements Runnable {
}
if (Arrays.equals(readUntilChar(' '),"ROXY".getBytes())) {
readUntilChar(' '); // proto
byte source[] = readUntilChar(' ');
byte[] source = readUntilChar(' ');
address = new String(source);
readUntilChar(' '); // destination IP
readUntilChar(' '); // source port
@ -63,10 +63,10 @@ public class ServerThread implements Runnable {
return;
}
PID = readPID();
if (PID.length == 0) {
socket.close();
return;
}
if (PID.length == 0) {
socket.close();
return;
}
}
switch (PID[0]) {
case MESSAGE -> {
@ -99,14 +99,14 @@ public class ServerThread implements Runnable {
}
private byte[] readUntilChar(char c) throws IOException {
byte b[] = new byte[256];
byte[] b = new byte[256];
int i;
for (i = 0;; i++) {
b[i] = (byte)input.read();
if (b[i] == c)
break;
}
byte r[] = new byte[i];
byte[] r = new byte[i];
System.arraycopy(b, 0, r, 0, i);
return r;
}