找到尝试连接到ServerSocket的程序IP [英] Find IP of program trying to connect to ServerSocket

查看:131
本文介绍了找到尝试连接到ServerSocket的程序IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

假设我有以下Java代码:

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

server.accept()被称为程序块,直到有人连接到我的服务器。有没有办法,能够找到连接到我服务器的程序/用户的IP?

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?

推荐答案

试试

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

这篇关于找到尝试连接到ServerSocket的程序IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆