java中怎么获取客户端的真实的ip和端口号

java中怎么获取客户端的真实的ip和端口号,第1张

public static String getHostIpAddress() {
String hostIp = "";
InetAddress netAddress = getInetAddress();
hostIp = getHostIp(netAddress);
return hostIp;
}
public static InetAddress getInetAddress() {
try {
return InetAddressgetLocalHost();
} catch (UnknownHostException e) {
Systemoutprintln("unknown host!");
}
return null;
}
public static String getHostIp(InetAddress netAddress) {
if (null == netAddress) {
return null;
}
String ip = netAddressgetHostAddress(); // get the ip address
return ip;
}
public static String getHostName(InetAddress netAddress) {
if (null == netAddress) {
return null;
}
String name = netAddressgetHostName(); // get the host address
return name;
}

Java中,使用proxy是通过设置java环境变量来实现的,也就是JVM的系统属性
我们需要设置三个属性,proxySet proxyHost和proxyPort。
proxySet 是个boolean类型的,可以设置为true或者false,true代表使用代理服务器
proxyHost 是代理服务器的IP地址
proxyPort 是代理服务器的端口地址。
我们有两种方法设置这种系统属性,
一种是命令行下运行Java程序的时候,通过参数付值
一种是在java源程序中设置。
第一种方法的格式如下:
java -DproxySet=true -DproxyHost=myProxyServercome -DproxyPort=80 MyJavaApp
第二种方法,就是直接在源程序中通过系统属性来写,如下:
import javautilProperties;


strProxy="22115567";
strPort="8080";
Properties systemProperties = SystemgetProperties();
systemPropertiessetProperty(">InetAddress ip=InetAddressgetByName(args[0]);//第一个输进来的是ip
int port = IntegerparseInt(args[1]);//e\第二个输进来的是端口号
这两个就是接收参数


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/12774068.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存