Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does WebSocket get the client's IP?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "WebSocket how to get the client's IP". In the daily operation, I believe many people have doubts about how to get the client's IP from WebSocket. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the questions of "how to get the client's IP from WebSocket". Next, please follow the editor to study!

Today, I use websocket. I hope the server can get the IP of the client when the connection is established. I saw that the api provided by the source code was not found. Later, after a meal of Baidu, I found a solution in stackoverflow. After testing, I summarized it. Here is the code:

Import java.lang.reflect.Field;import java.net.InetSocketAddress;import javax.websocket.RemoteEndpoint.Async;import javax.websocket.Session; public class WebsocketUtil {public static InetSocketAddress getRemoteAddress (Session session) {if (session = = null) {return null;} Async async = session.getAsyncRemote () / / valid in Tomcat 8.0.x version / / InetSocketAddress addr = (InetSocketAddress) getFieldInstance (async, "base#sos#socketWrapper#socket#sc#remoteAddress"); / / valid InetSocketAddress addr = (InetSocketAddress) getFieldInstance (async, "base#socketWrapper#socket#sc#remoteAddress") above Tomcat 8.5; return addr } private static Object getFieldInstance (Object obj, String fieldPath) {String fields [] = fieldPath.split ("#"); for (String field: fields) {obj = getField (obj, obj.getClass (), field); if (obj = = null) {return null }} return obj;} private static Object getField (Object obj, Class clazz, String fieldName) {for (; clazz! = Object.class; clazz = clazz.getSuperclass ()) {try {Field field Field = clazz.getDeclaredField (fieldName); field.setAccessible (true); return field.get (obj);} catch (Exception e) {}} return null;}}

Invoke the test:

/ * * method called successfully for connection establishment * / @ OnOpen public void onOpen (Session session) {InetSocketAddress remoteAddress = WebsocketUtil.getRemoteAddress (session); System.out.println ("A new connection is added!" + remoteAddress);}

Output result:

At this point, the study on "how to get the client-side IP of WebSocket" 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report