如何获取客户端端口和ip地址在客户端socket程序中的java [英] how to get know the client port and ip address in client socket program in java
本文介绍了如何获取客户端端口和ip地址在客户端socket程序中的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用socket在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);
这篇关于如何获取客户端端口和ip地址在客户端socket程序中的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文