Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn Java class into one of its subclasses (SocketAddress and InetSocketAddress)

I am trying to get the IP of a socket connection in string form.

I am using a framework, which returns the SocketAddress of the received message. How can i transform it to InetSocketAddress or InetAddress?

like image 436
vasion Avatar asked Dec 18 '22 02:12

vasion


1 Answers

If your certain that the object is an InetSocketAddress then simply cast it:

SocketAddress sockAddr = ...
InetSocketAddress inetAddr = (InetSocketAddress)sockAddr;

You can then call the getAddress() method on inetAddr to get the InetAddress object associated with it.

like image 76
Jared Russell Avatar answered Dec 19 '22 15:12

Jared Russell