Search code examples
javasocketsserversocket

server socket to accept connection from particular ip java


I have a serversocket running on a port say 7761 in my server with ip say 10.2.110.43 now there are many client that run on different servers waiting for connection on port 7761, and write data in ascii format to that port.

I want the serversocket to verify the client-ipadress and then accept connection from client.

Is there a way to do that?


Solution

  • If you don't mind running under a SecurityManager and the list of IP addresses is static, you can accomplish this via the security.policy file. Just grant SocketPermission"accept" to only those IP addresses you want to accept connections from. However doing it in code or the firewall as suggested in another answers is probably preferable.