Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

4 changed files with 13 additions and 18 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");
isProxied = args[2].equals("on") ? true : false;
try {
server = new CrabServer(Integer.parseInt(args[1]), isProxied);
} catch (NumberFormatException e) {
@ -42,12 +42,15 @@ public class Main {
return;
}
} else {
System.err.println("Not enough arguments.");
System.err.println("Now enough arguments.");
return;
}
server.run();
}
default -> System.err.println("Unknown argument");
default -> {
System.err.println("Unknown argument");
return;
}
}
}

View File

@ -6,7 +6,6 @@ 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 {
@ -93,11 +92,8 @@ 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,8 +61,4 @@ 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;
}