
public class NetTool{
InetAddress myIPaddress=null;
InetAddress myServer=null;
public static void main( String args[]){
NetTool mytool;
mytool=new NetTool();
Systemoutprintln("Your host IP is: " + mytoolgetMyIP());
Systemoutprintln("The Server IP is :" +mytoolgetServerIP());
}
//取得LOCALHOST的IP地址
public InetAddress getMyIP() {
try { myIPaddress=InetAddressgetLocalHost();}
catch (UnknownHostException e) {}
return (myIPaddress);
}
//取得 >代码亲测可用:
import javanetInetAddress;
import javanetUnknownHostException;
public class TestInetAddress {
InetAddress myIpAddress = null;
InetAddress[] myServer = null;
public static void main(String args[]) {
TestInetAddress address = new TestInetAddress();
Systemoutprintln("Your host IP is: " + addressgetLocalhostIP());
String domain = ">package test5;
public interface A {
void setAll(int ID,String IP);
String getAll();
}
package test5;
public class B implements A{
String IP;
int ID;
public void setAll(int ID,String IP) {
thisID=ID;
thisIP=IP;
}
public String getAll() {
return "ID为: "+ID+"\t\t"+"IP为: "+IP;
}
public static void main(String[] args) {
B b=new B();
bsetAll(1, "19216811");
Systemoutprintln(bgetAll());
}
}下面有一篇文章,介绍若何读取物理网卡的地址 ,同样的
你可以用这个方法读取你所需要的本机IP地址
=======================================================
J2SE50新特性之ProcessBuilder
这个例子使用了J2SE50的ProcessBuilder类执行外部的程序,相对于 Runtimeexec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
package comkuaffjdk5package;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
public class ProcessBuilderShow
{
public static List getPhysicalAddress()
{
Process p = null;
//物理网卡列表
List address = new ArrayList();
try
{
//执行ipconfig /all命令
p = new ProcessBuilder("ipconfig", "/all")start();
}
catch (IOException e)
{
return address;
}
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
//读取进程输出值
InputStream in = pgetInputStream();
try
{
while (inread(b)>0)
{
sbappend(new String(b));
}
}
catch (IOException e1)
{
}
finally
{
try
{
inclose();
}
catch (IOException e2)
{
}
}
//以下分析输出值,得到物理网卡
String rtValue = sbsubstring(0);
int i = rtValueindexOf("Physical Address :");
while(i>0)
{
rtValue = rtValuesubstring(i + "Physical Address :"length());
addressadd(rtValuesubstring(0,18));
i = rtValueindexOf("Physical Address :");
}
return address;
}
public static void main(String[] args)
{
List address = ProcessBuilderShowgetPhysicalAddress();
for(String add:address)
{
Systemoutprintf("物理网卡地址:%s%n", add);
}
}
}java获取本机的外网ip示例:
import javaioIOException;
import javaioInputStream;
import javanet>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)