开发者

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

开发者 https://www.devze.com 2023-02-13 02:47 出处:网络
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 re

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

精彩评论

暂无评论...
验证码 换一张
取 消