开发者

Is there a way that server know the connecting client's hostname?

Client is given a server's hostname, so that client can connect to the server with the given hostname.

//client side
host = new Inet开发者_如何转开发SocketAddress ( args[0], 50000); // args[0] = server's address
sockfd = new Socket(host.getHostName(),host.getPort());



//server side
sockfd = new ServerSocket(50000);
Socket clientfd= sockfd.accept ();

When client connects to the server A like above, is there a way for server A to know the client's hostname? Because server A has to tell the other server B to give that client some messages.

If there isn't, how should connected server A notify the server B about connecting to the client?

Client also has ServerSocket which can receive connection from server B


server A can use getInetAddress and getHostName to determine the client's hostname. The hostname shouldn't be needed, though, because you also can get the client IP from the Socket:

Socket clientfd = sockfd.accept();
InetAddress clientIA = clientfd.getInetAddress();
String clientHN = clientIA.getHostName();
byte[] clientIP = clientIA.getAddress();

The networking issues Greg mentioned are still valid, however; having a server connect back to any given client may be difficult or impossible.


One approach is to have the client send its hostname to the server A, which server A would then pass on to server B, so that server B can send the client messages. You would want to make sure that the client doesn't lie about its own hostname, and that the DNS is set up to correctly map the name back to the client's address (this is usually outside the client machine's direct control).

Note that there will generally be issues with having a "server" connect back to a "client", particularly with network issues such as NAT and firewalls. Probably a better idea is to have the client make a second connection to server B, so that server B can communicate with the client directly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜