【问题标题】:Find IP of program trying to connect to ServerSocket查找尝试连接到 ServerSocket 的程序的 IP
【发布时间】:2011-06-27 07:56:29
【问题描述】:

虽然我搜索过,但找不到答案。

假设我有以下 Java 代码:

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

当server.accept() 被调用时,程序会阻塞,直到有人连接到我的服务器。有没有办法可以找到连接到我的服务器的程序/用户的 IP?

【问题讨论】:

    标签: java sockets client ip inetaddress


    【解决方案1】:

    试试

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

    【讨论】:

    • 这似乎又回到了前面。 'from' ip:port 由 getInetAddress() 给出:getPort()。本地 ip:port 由 getLocalAddress() 给出:getLocalPort()。
    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多