尽管我进行了搜索,但找不到答案。

假设我有以下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?

最佳答案

尝试

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

08-08 00:33