Limit TCP requests per IP

Posted by asmo on Stack Overflow See other posts from Stack Overflow or by asmo
Published on 2010-06-02T07:53:23Z Indexed on 2010/06/02 7:53 UTC
Read the original article Hit count: 227

Filed under:
|
|
|

Hello!

I'm wondering how to limit the TCP requests per client (per specific IP) in Java. For example, I would like to allow a maximum of X requests per Y seconds for each client IP. I thought of using static Timer/TimerTask in combination with a HashSet of temporary restricted IPs.

  private static final Set<InetAddress> restrictedIPs = Collections.synchronizedSet(new HashSet<InetAddress>());

private static final Timer restrictTimer = new Timer();

So when a user connects to the server, I add his IP to the restricted list, and start a task to unrestrict him in X seconds.

restrictedIPs.add(socket.getInetAddress());

restrictTimer.schedule(new TimerTask() { public void run() { restrictedIPs.remove(socket.getInetAddress()); }

}, MIN_REQUEST_INTERVAL);

My problem is that at the time the task will run, the socket object may be closed, and the remote IP address won't be accessible anymore...

Any ideas welcomed! Also, if someone knows a Java-framework-built-in way to achieve this, I'd really like to hear it.

© Stack Overflow or respective owner

Related posts about java

Related posts about tcp