
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;
}
Systemoutprintln("IP地址:"+InetAddressgetLocalHost());
127001是默认的本机IP,其实就是相当于把本机当成一个局域网,它还有个名字:localhost
方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmdexe /c ipconfig | findstr IPv4";
try {
Process p = RuntimegetRuntime()exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(pgetInputStream()));
String line = null;
while((line = brreadLine()) != null){
line = linesubstring(linelastIndexOf(":")+2,linelength());
sbappend(line);
}
brclose();
pdestroy();
} catch (IOException e) {
eprintStackTrace();
}
return sbtoString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterfacegetNetworkInterfaces();
while (enhasMoreElements()) {
NetworkInterface intf = (NetworkInterface) ennextElement();
Enumeration<InetAddress> enumIpAddr = intfgetInetAddresses();
while (enumIpAddrhasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddrnextElement();
if (!inetAddressisLoopbackAddress() && !inetAddressisLinkLocalAddress()
&& inetAddressisSiteLocalAddress()) {
sbappend(inetAddressgetHostAddress()toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sbtoString();
}
java获取本机的外网ip示例:
import javaioIOException;
import javaioInputStream;
import javanet>
网络通信可分为两种模式:TCP有连接的通信
UDP无连接的通信
依照上述的问题我可以理解为你是要进行有连接的通信。是要经过三次握手才能实现可靠的连接
第一次:建立连接时三次握手,客户端发送syn包(syn=j)到服务器,并进入SYN_SENT状态,等待服务器确认;SYN(Synchronize Sequence Numbers)同步序列号。
服务器端即可使用:
Socket s=serverSocketaccept();
String clientIP=sgetInetAddress()toString();
这就实现了java服务器端获取到连接此服务器的客户端的IP了,你若想实现获取所有连接此服务器的客户端的IP,你可以把每次accept()返回的socket放进全局的Vector里面,然后在遍历这个vector方法就同上代码咯……
为适应不同的网络情况,提供这个类:看下面的代码吧,Copy过去就能用。
import javaxservlet>
网络通信可分为两种模式:TCP有连接的通信
UDP无连接的通信
依照上述的问题我可以理解为你是要进行有连接的通信。是要经过三次握手才能实现可靠的连接
第一次:建立连接时三次握手,客户端发送syn包(syn=j)到服务器,并进入SYN_SENT状态,等待服务器确认;SYN(Synchronize Sequence Numbers)同步序列号。
服务器端即可使用:
Socket s=serverSocketaccept();
String clientIP=sgetInetAddress()toString();
这就实现了java服务器端获取到连接此服务器的客户端的IP了,你若想实现获取所有连接此服务器的客户端的IP,你可以把每次accept()返回的socket放进全局的Vector里面,然后在遍历这个vector方法就同上代码咯……
import javanetInetAddress;
import javautilEnumeration;
import javanetNetworkInterface;
import javautil;
public class ipdisplay {
/
@param args
@throws Exception
/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String allipaddress;
ArrayList ar = new ArrayList();
Enumeration netInterfaces = NetworkInterfacegetNetworkInterfaces();
while (netInterfaceshasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfacesnextElement();
Enumeration cardipaddress = nigetInetAddresses();
InetAddress ip = (InetAddress) cardipaddressnextElement();
if(!ipgetHostAddress()equalsIgnoreCase("127001") )
{ aradd(nigetName()+":");
allipaddress=ipgetHostAddress();
while(cardipaddresshasMoreElements())
{
ip = (InetAddress) cardipaddressnextElement();
allipaddress=allipaddress+" , "+ipgetHostAddress();
}
aradd(allipaddress);
}
else
continue;
}
for(int i=0;i<arsize();)
{
Systemoutprintln(arget(i++));
}
}
}
以上就是关于java中怎么获取客户端的真实的ip和端口号全部的内容,包括:java中怎么获取客户端的真实的ip和端口号、java获取IP 为什么是127.0.0.1、java中获取本地IP地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)