Reputation: 405
Sorry, simple question...but I can't find the answer anywhere using google or in textbooks! I have a simple server, to which a user connects via a socket in java. I want to use this Java socket to retrieve the users client name, is this possible ?
I know I can use the getInetAddress() method to returns the address to which the socket is connected, but that isn't really what I want. Is there any easy way of doing this ?
Thank you.
Upvotes: 5
Views: 14813
Reputation: 109037
getInetAddress().getHostName()
EDIT: is your code structured like this?
import java.io.IOException;
import java.net.*;
public class Test {
public void foo() throws IOException{
ServerSocket server = null; //Initialize server socket here.
Socket client = server.accept();
String hostName = client.getInetAddress().getHostName();
}
}
Upvotes: 8