gpt4 book ai didi

java - java中如何获取客户端socket程序中的客户端端口和IP地址

转载 作者:行者123 更新时间:2023-12-01 14:59:33 27 4
gpt4 key购买 nike

我使用套接字在 java 中创建了多个客户端-服务器通信。我有一个服务器和一个客户端。我想用多个客户端测试我的程序。我计划创建一个动态创建 ip 和端口的模拟器。为此我只想知道如何在套接字程序中设置客户端 ip 和端口。谁能帮助我..我使用 InetAddress.getByName 来获取客户端地址。

 public Socket(Proxy proxy)
{
if (proxy != null && proxy.type() == Proxy.Type.SOCKS)
{
SecurityManager security = System.getSecurityManager();
InetSocketAddress epoint = (InetSocketAddress) proxy.address();
if (security != null) {
if (epoint.isUnresolved())
epoint = new InetSocketAddress(epoint.getHostName(), epoint.getPort());
if (epoint.isUnresolved())
security.checkConnect(epoint.getHostName(),
epoint.getPort());
else
security.checkConnect(epoint.getAddress().getHostAddress(),
epoint.getPort());
}
impl = new SocksSocketImpl(proxy);
impl.setSocket(this);
} else {
if (proxy == Proxy.NO_PROXY) {
if (factory == null) {
impl = new PlainSocketImpl();
impl.setSocket(this);
} else
setImpl();
} else
throw new IllegalArgumentException("Invalid Proxy");
}
}

最佳答案

试试这个

clientString = "Remote client: " + socket.getRemoteSocketAddress().toString().substring(1);

关于java - java中如何获取客户端socket程序中的客户端端口和IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13876303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com