This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes ServerSocket deny connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "ServerSocket deny connection" Watch "ServerSocket deny connection" New topic
Author

ServerSocket deny connection

Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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

Joined: Sep 24, 2009
Posts: 60
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServerSocket deny connection
 
Similar Threads
What is the difference in these two methods : Socket.bind() , Socket.connect()
force close server socket while waits for connection
ServerSocket - setSoTimeout
ServerSocket question
get client's IP adress without servlets or jsp?