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.
getInetAddress().getHostName()
Reference Link
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();
}
}
精彩评论