Find the IP address of a program trying to connect to ServerSocket

Although I searched about this, I could not find the answer.

Let's say I have the following Java code:

ServerSocket serve = null; try { server = new ServerSocket(5567); } catch (IOException e) { System.err.println("Problem with port 5567"); System.exit(1); } Socket clientSocket = null; try { clientSocket = server.accept(); } catch (IOException e) { System.exit(1); } 

When server.accept() is called by program blocks until someone connects to my server. Is there any way to find the IP address of the program / user that connects to my server?

+4
source share
1 answer

Try

 Socket clientSocket = null; try { clientSocket = server.accept(); System.out.println("Connected from " + clientSocket .getInetAddress() + " on port " + clientSocket .getPort() + " to port " + clientSocket .getLocalPort() + " of " + clientSocket .getLocalAddress()); } catch (IOException e) { System.exit(1); } 
+4
source

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


All Articles