java socket: listen before accept?

假装没事ソ 提交于 2019-12-06 07:25:16

Without using a SecurityManager, you can only accept, then check the incoming IP address, and drop the connection if it's on the banned list. With a SecurityManager, you can have it throw a SecurityException, and give it special handling (such as logging the connection request in a security log in the app).

If you don't want these connections to even hit your server (which I'm guessing is the point), you really want to block it at the firewall level. Once the connection hits your app, it's already taken up the memory and resources necessary to talk to your application, so at that point you've already lost the DOS/DDOS game.

Dropping it before or after an accept doesn't matter, because the connection has already hit the application level of your app. Even with a SecurityManager, it's still increasing the load on your server.

You might be able to, but you wouldn't be able to stop them from accessing your system in your Java code anyway.

Your best bet would be, instead of using Java, use something like iptables or some other firewall to drop their packets right away.

The boys over at serverfault can probably help with that better than SO.

You can use a SecurityManager with suitable accept SocketPermissions defined, if you can abide having the addresses defined in a .policy file, or writing your own Policy object.

EDIT: but I have to admit that a firewall would be a better solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!