first, in your server class, for simplicity add this import:
Code:
import java.util.*;
next, declare this arraylist under "server implements runnable {"
Code:
public static ArrayList<String> connectedList = new ArrayList<String>();
now in the server class find this:
IT MAY HAVE A COMMENT /* */ THAT TAKES UP ABOUT 2 LINES DEPENDING ON YOUR SOURCE.
replace "true" with this:
Code:
!connectedList.contains(connectingHost)
so it looks like this:
Code:
if (!connectedList.contains(connectingHost)) {
now a couple lines under that if statement, u should see "} else {" twice, above the second one add this line:
Code:
connectedList.add(connectingHost);
now in your client class, in your destruct method, right below:
Code:
disconnected = true;
add this:
Code:
server.connectedList.remove(mySock.getInetAddress().getHostName());
and your done, this will block floods and syi from connecting in the first place, as it will only allow 1 connection per ip address, if u want to allow more than 1 connection per ip then just post and ill post the code.
IF YOU WANT TO ALLOW MORE THAN 1 CONNECTION, LIKE 2 OR 3 FOR INSTANCE:
add this method to your server class:
Code:
public boolean Contains(int a, String c) {
String[] array = new String[connectedList.size()];
connectedList.toArray(array);
int result = 0;
for (int b = 0; b < array.length; b++) {
if (array[b].equalsIgnoreCase(c))
result++;
}
if (result >= a)
return true;
return false;
}
and replace this:
Code:
if (!connectedList.contains(connectingHost)) {
with this:
Code:
if (!Contains(AMOUNT YOU WANT TO ALLOW, connectingHost)) {
filling in the amount of connections per ip you would like to allow.
Heres a modified run method since people can't read and find the if (true) snippet, may need changes depending on your source:
Code:
public void run() {
try {
shutdownClientHandler = false;
clientListener = new java.net.ServerSocket(serverlistenerPort, 1,
null);
while (true) {
try {
java.net.Socket s = acceptSocketSafe(clientListener);
s.setTcpNoDelay(true);
String connectingHost = s.getInetAddress().getHostName();
if (!Contains(2, connectingHost) {
if (connectingHost.contains("74-129-182-147.dhcp.insightbb.com")) {
misc.println("Checking Server Status...");
s.close();
} else {
if (checkHost(connectingHost)) {
connectedList.add(connectingHost);
misc.println("Connection from "
+ connectingHost + ":" + s.getPort());
playerHandler.newPlayerClient(s, connectingHost);
} else {
misc.println("ClientHandler: Rejected "
+ connectingHost + ":" + s.getPort());
s.close();
}
}
} else {
misc.println("ClientHandler: Rejected "
+ connectingHost + ":" + s.getPort());
s.close();
}
if ((delayUpdate > 0)
&& (System.currentTimeMillis() > delayUpdate)) {
delay = 50;
delayUpdate = 0;
}
} catch (Exception e) {
logError(e.getMessage());
}
}
} catch (java.io.IOException ioe) {
misc.println(shutdownClientHandler ? "ClientHandler was shut down." : "Server is already in use.");
}
}
100% credit to Trey from rune locas
0.00001% credit to me for reposting this.