In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to get the current server or user ip and port". In the daily operation, I believe that many people have doubts about how to obtain the current server or user ip and port. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to get the current server or user ip and port". Next, please follow the editor to study!
Get the current server address by request (request). I'm sure you know all about it.
Request.getHeader ("User-Agent"); / / get the client's system version request.getRemoteAddr (); / / get the client's IP request.getRemoteHost () / / get the client's hostname request.getRemotePort (); / / get the client's port request.getRemoteUser (); / / get the client's user request.getLocalAddr () / / get the server IP request.getLocalPort (); / / get the server port
However, the server address can be obtained without request and tested by either of the following two methods
/ / method 1 String SERVER_IP = ""; try {Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces (); InetAddress ip = null; while (netInterfaces.hasMoreElements ()) {NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement (); ni.getInterfaceAddresses (); ip = (InetAddress) ni.getInetAddresses (). NextElement () SERVER_IP = ip.getHostAddress (); if (! ip.isSiteLocalAddress () & &! ip.isLoopbackAddress () & & ip.getHostAddress (). IndexOf (":") = =-1) {SERVER_IP = ip.getHostAddress (); break } else {ip = null;} catch (Exception e) {e.printStackTrace ();} / / method 2 InetAddress addr = null; try {addr = InetAddress.getLocalHost ();} catch (Exception e) {e.printStackTrace () } byte [] ipAddr = addr.getAddress (); String ipAddrStr = ""; for (int I = 0; I
< ipAddr.length; i++) { if (i >0) {ipAddrStr + = ".";} ipAddrStr + = ipAddr [I] & 0xFF;} System.out.println (ipAddrStr)
If multiple applications (multi-ports) are deployed under the same server, how to determine the port has not been found.
For example: if there are multiple service
Personal use method to obtain the project root directory address to determine the service deployment address
/ / get the root directory path String rootPath=getClass () .getResource ("/") .getFile () .toString ()
If you use a reverse proxy to get the user ip
Public static String getRemoteIpAddress () {HttpServletRequest request = getHttpServletRequest (); String ip = request.getHeader ("x-forwarded-for"); if (ip = = null | | ip.length () = = 0 | | "unknown" .equalsIgnoreCase (ip)) {ip = request.getHeader ("Proxy-Client-IP") } if (ip = = null | | ip.length () = = 0 | | "unknown" .equalsIgnoreCase (ip)) {ip = request.getHeader ("WL-Proxy-Client-IP");} if (ip = = null | | ip.length () = = 0 | "unknown" .equalsIgnoreCase (ip)) {ip = request.getRemoteAddr () } if (ip = = null | | ip.length () = = 0 | | "unknown" .equalsIgnoreCase (ip)) {ip = request.getHeader ("http_client_ip");} if (ip = = null | | ip.length () = = 0 | | "unknown" .equalsIgnoreCase (ip)) {ip = request.getHeader ("HTTP_X_FORWARDED_FOR") } if (ip! = null & & ip.indexOf (",")! =-1) {ip= ip.substring (ip.lastIndexOf (",") + 1, ip.length ()). Trim ();} if ("0 ip=" 127.0.0.1 ";} return ip At this point, the study on "how to get the current server or user ip and port" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.