Skip to content

Instantly share code, notes, and snippets.

@petermd
Created March 27, 2014 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petermd/9806313 to your computer and use it in GitHub Desktop.
Save petermd/9806313 to your computer and use it in GitHub Desktop.
getting remote ip from ServerWebSocket
public static
InetSocketAddress getRemoteAddress (final ServerWebSocket ws)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
{
InetSocketAddress addr = null;
final WebSocketImplBase requestWS = (WebSocketImplBase)ws;
Field f = WebSocketImplBase.class.getDeclaredField("conn"); //NoSuchFieldException
f.setAccessible(true);
ConnectionBase connectionBase = (ConnectionBase) f.get(requestWS); //IllegalAccessException
f = ConnectionBase.class.getDeclaredField("channel"); //NoSuchFieldException
f.setAccessible(true);
NioSocketChannel channel = (NioSocketChannel) f.get(connectionBase); //IllegalAccessException
if (channel != null) {
addr = channel.remoteAddress();
}
return addr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment