Thread: Error on one connection per IP

Results 1 to 2 of 2
  1. #1 Error on one connection per IP 
    dark mager
    Guest
    Hey guys I get this Error:



    Code:
    ----------------------
    --Initiating Compile--
    ----------------------
    server.java:319: 'catch' without 'try'
                                    } catch (Exception e) {
                                      ^
    server.java:319: ')' expected
                                    } catch (Exception e) {
                                                      ^
    server.java:319: not a statement
                                    } catch (Exception e) {
                                            ^
    server.java:319: ';' expected
                                    } catch (Exception e) {
                                                        ^
    server.java:330: illegal start of expression
    private final ConnectedList list = ConnectedList.getInstance();
    ^
    server.java:276: 'try' without 'catch' or 'finally'
                    try {
                    ^
    server.java:333: reached end of file while parsing
    }
     ^
    7 errors
    ----------------------
    --Compiling complete--
    ----------------------
    Anyone know what i do to fix it?


    The thread or tut i was following is here:
    [Only registered and activated users can see links. ]

    Thanks if you can help
    Reply With Quote  
     

  2. #2  
    Registered Member
    Omer5454's Avatar
    Join Date
    Sep 2009
    Posts
    139
    Thanks given
    0
    Thanks received
    6
    Rep Power
    246
    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:
    Code:
    if (true) {
    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.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •