2011-02-15 157 views
4

儘管我搜索了它,但我找不到答案。查找試圖連接到ServerSocket的程序IP IP地址

比方說,我有以下的Java代碼:

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); 
    } 

server.accept()被稱爲程序塊,直到有人連接到我的服務器。有沒有辦法,能夠找到連接到我的服務器的程序/用戶的IP?

回答

4

嘗試

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); 
    } 
+0

這似乎又回到眼前。 'from'ip:port由getInetAddress():getPort()給出。本地ip:端口由getLocalAddress():getLocalPort()給出。 – EJP 2011-02-15 03:54:07