how to get know the client port and ip address in client socket program in java(如何在java中的客户端套接字程序中知道客户端端口和IP地址)
问题描述
我使用套接字在 java 中创建了一个多客户端-服务器通信.我有一个服务器和一个客户端.我想用多个客户端测试我的程序.我计划创建一个动态创建 ip 和端口的模拟器.为此我只想知道如何在套接字程序中设置客户端 ip 和端口.任何人都可以帮助我.. 我使用 InetAddress.getByName 来获取客户端地址.
I created a multiple client- server communication in java using socket. i have a single server and a client. i want to test my program with multiple clients. i m planning to create a simulator which dynamically create ip and port.. for that i just want to know how to set the client ip and port in the socket program. can anyone help me.. i use InetAddress.getByName to get the client address.
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中的客户端套接字程序中知道客户端端口和IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在java中的客户端套接字程序中知道客户端端口和IP地址
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01