Find IP of program trying to connect to ServerSocket

自古美人都是妖i 提交于 2019-12-10 17:29:44

问题


Although I searched about it I couldn't find an answer.

Let's say I have the following Java code:

    ServerSocket serve = null;

    try {
        server = new ServerSocket(5567);
    } catch (IOException e) {
        System.err.println("Problem with port 5567");
        System.exit(1);
    }

    Socket clientSocket = null;
    try {
        clientSocket = server.accept();
    } catch (IOException e) {
        System.exit(1);
    }

When server.accept() is being called the program blocks until someone connects to my server. Is there a way, to be able to find the IP of the program/user who connects to my server?


回答1:


Try

Socket clientSocket = null;
    try {
        clientSocket = server.accept();
        System.out.println("Connected from " + clientSocket .getInetAddress() + " on port "
             + clientSocket .getPort() + " to port " + clientSocket .getLocalPort() + " of "
             + clientSocket .getLocalAddress());
    } catch (IOException e) {
        System.exit(1);
    }


来源:https://stackoverflow.com/questions/4999408/find-ip-of-program-trying-to-connect-to-serversocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!