michael corrigan
michael corrigan

Reputation: 405

How do I get the client name from a socket in Java?

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

Answers (1)

Bala R
Bala R

Reputation: 109037

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();
        }
}

Upvotes: 8

Related Questions