Server socket to accept connection from specific ip java

I have a server server running on a port, for example 7761 on my server with ip say 10.2.110.43 now there are many clients that run on different servers waiting for connections on port 7761 and write ascii data to this port.

I want serverocket to check the ipadress client and then accept the client connection.

Is there any way to do this?

+4
source share
3 answers

If you do not mind running under SecurityManager , and the list of IP addresses is static, you can do this through the security.policy file. Just highlight SocketPermission "accept" only for the IP addresses with which you want to accept connections. However, doing this in code or a firewall, as suggested in other answers, is probably preferable.

+5
source

In the following code, we cannot check the address of the instances before accept() , but after:

 Socket client = serverSocket.accept() if( acceptedClients.contains( client.getInetAddress()) { ... } else { client.close(); } 

With acceptedClients compilation of the famous InetAddress.

+4
source

If you do not want the connection to reach your Java ServerSocket#accept() , if it is not associated with a specific IP address, you need to configure your firewall to do this.

You can always check the IP address after establishing a connection and immediately close it if it does not match the correct IP address.

+3
source

Source: https://habr.com/ru/post/1441978/


All Articles