In my app, I create a ServerSocket, and wait for connections:
while(isRunning) {
try {
socket = serverSocket.accept();
I then try to get the remote IP of the resulting socket:
socket.getInetAddress().getHostAddress();
However, this seems to only return an IPv6 address.
For my purposes, I believe I need an IPv4. Is there some way to get an IPv4 address from a socket?
If the client has connected to an IPv6 address (i.e., if your server is IPv6 hosted), then you’ll only have the IPv6 address. You’ll need to host you server in IPv4 space as well (or instead of), then have the client connect to the IPv4 address in order to get the IPv4 address of the client.
You may be able to do a lookup via DNS, but this won’t be reliable (i.e., do reverse lookup of IPv6 address to get hostname, then do a forward lookup, looking for A records on the host). This assumes that reverse DNS has been set up though for the address, which isn’t guaranteed.