• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServerSocket deny connection

 
Doua Beri
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a ServerSocket and I accept connection using the accept() method. However I want to deny the connection if the request comes from a certain IP. I have searched but didn't find any related method. Can you help me? Thanks in advance
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After accepting check out the Socket's remote address (check out java.net.Socket), and close the socket if you don't want the connection.
 
Doua Beri
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:After accepting check out the Socket's remote address (check out java.net.Socket), and close the socket if you don't want the connection.


Thanks for the quick reply. So basically I need accept the connection and then make the verification and after that I close the socket. There is no other way without accepting the connection?
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in Java. You can configure your firewall to block all connections from that host, but you can't do that from Java.
 
Doua Beri
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Not in Java. You can configure your firewall to block all connections from that host, but you can't do that from Java.


Yes. I searched a lot in the last hour and didn't find any solution. However on the implAccept(socket); which can be override there is the following code



I'm not sure if this is a solution and I looked for an example of using SecurityManager or SocketPermission but without any luck.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic