Client is given a server’s hostname, so that client can connect to the server with the given hostname.
//client side
host = new InetSocketAddress ( 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:
The networking issues Greg mentioned are still valid, however; having a server connect back to any given client may be difficult or impossible.