java 获取局域网内IP

java,如何获取局域网内所有的ip呢?

如何扫描局域网内的所有ip呢?

代码如下:

package com.jn.lan;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.StringTokenizer;
import java.util.Vector;

import com.common.util.SystemHWUtil;
import com.string.widget.util.ValueWidget;

public class LANIP {
	public static InetAddress[] getAllOnline() {
		// TODO Auto-generated method stub
		Vector v = new Vector(50);
		try {
			// Process process1 =
			// Runtime.getRuntime().exec("ping -w 2 -n 1 192.168.1.%i");
			// process1.destroy();
			Process process = Runtime.getRuntime().exec("arp -a");
			InputStreamReader inputStr = new InputStreamReader(
					process.getInputStream(), "GBK");
			BufferedReader br = new BufferedReader(inputStr);
			String temp = "";
			br.readLine();
			br.readLine();
			br.readLine();// 此后开始读取IP地址,之前为描述信息,忽略。
			while ((temp = br.readLine()) != null) {
				System.out.println(temp);
				if (!ValueWidget.isNullOrEmpty(temp)) {
					StringTokenizer tokens = new StringTokenizer(temp);
					String x;
					InetAddress add=null;
					try {
						add = InetAddress.getByName(x = tokens
								.nextToken());
					} catch (java.net.UnknownHostException e) {
						continue;
					}
					// System.out.println(x);
					v.add(add);
					// System.out.println(add);
				}
			}
			System.out.println(SystemHWUtil.DIVIDING_LINE);
			v.add(InetAddress.getLocalHost());
			process.destroy();
			br.close();
			inputStr.close();
		} catch (Exception e) {
			System.out.println("可能是网络不可用。");
			e.printStackTrace();
		}
		int cap = v.size();
		InetAddress[] addrs = new InetAddress[cap];
		for (int i = 0; i < cap; i++) {
			addrs[i] = (InetAddress) v.elementAt(i);
			 System.out.println(addrs[i]);
		}
		return addrs;

	}

	public static void main(String args[]) {
		InetAddress[] i = new LANIP().getAllOnline();
	}
}

 运行结果:

  169.254.255.255       ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  226.81.9.8            01-00-5e-51-09-08     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        

接口: 169.254.190.162 --- 0xd
  Internet 地址         物理地址              类型
  169.254.255.255       ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  226.81.9.8            01-00-5e-51-09-08     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        

接口: 192.168.1.105 --- 0x14
  Internet 地址         物理地址              类型
  192.168.1.1           00-21-5d-3c-79-16     动态        
  192.168.1.106         b8-76-3f-a6-a8-e9     动态        
  192.168.1.109         60-d8-19-14-6c-55     动态        
  192.168.1.110         00-21-5d-3c-79-16     动态        
  192.168.1.255         ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        

接口: 192.168.137.1 --- 0x15
  Internet 地址         物理地址              类型
  192.168.137.182       84-7a-88-cf-87-35     动态        
  192.168.137.255       ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        
---------------------------------------
/169.254.255.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/226.81.9.8
/239.255.255.250
/255.255.255.255
/169.254.255.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/226.81.9.8
/239.255.255.250
/255.255.255.255
/192.168.1.1
/192.168.1.106
/192.168.1.109
/192.168.1.110
/192.168.1.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/239.255.255.250
/255.255.255.255
/192.168.137.182
/192.168.137.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/239.255.255.250
/255.255.255.255
wh-THINK/169.254.86.27

 

展开阅读全文

获取局域网内所有pc的ip

11-30

从网上找到的代码:rn[code=C/C++]rnvoid CGetLANAllIpDlg::FindAllComputer(CStringArray& MyList)rn rn MyList.RemoveAll();rn CString StrTemp; rn struct hostent *host;rn struct in_addr *ptr; rn DWORD dwScope = RESOURCE_CONTEXT;rn NETRESOURCE *NetResource = NULL;rn HANDLE hEnum;rn WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum );rn WSADATA wsaData;rn //开始搜索网络资源rn WSAStartup(MAKEWORD(1,1),&wsaData);rn //如果句柄有效rn if ( hEnum ) rn rn DWORD Count = 0xFFFFFFFF;rn DWORD BufferSize = 2048;rn LPVOID Buffer = new char[2048];rn //调用WSAStartup后调用WNetEnumResource做进一步的搜索工作rn WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );rn NetResource = (NETRESOURCE*)Buffer;rn char StrHostName[200];rn for ( unsigned int i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ ) rn rn if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) rn rn if ( NetResource->lpRemoteName )rn rn CString strFullName = NetResource->lpRemoteName;rn if ( 0 == strFullName.Left(2).Compare("\\\\") ) rn strFullName = strFullName.Right(strFullName.GetLength()-2); rn //获得主机名rn gethostname( StrHostName, strlen( StrHostName ) );rn //由主机名获得跟它对应的主机信息rn host = gethostbyname(strFullName);rn if(host == NULL) continue; rn ptr = (struct in_addr *) host->h_addr_list[0]; rn // 提取IP地址信息,地址形式如下: 211.40.35.76 rn int a = ptr->S_un.S_un_b.s_b1; // 211 rn int b = ptr->S_un.S_un_b.s_b2; // 40rn int c = ptr->S_un.S_un_b.s_b3; // 35rn int d = ptr->S_un.S_un_b.s_b4; // 76rn StrTemp.Format("%s : %d.%d.%d.%d",strFullName,a,b,c,d);rn // 加入到链表中rn MyList.Add(StrTemp);rn rn rn rn delete Buffer;rn // 结束搜索工作rn WNetCloseEnum( hEnum ); rn rn // 卸载Winsock.dllrn WSACleanup();rnrn[/code]rn从网上找到的我就能够运行,自己写了一下(就是给这个函数copy过去),就出现了连接的错误!rn[code=C/C++]rn1>GetLANAllIpDlg.obj : error LNK2019: 无法解析的外部符号 _WNetCloseEnum@4,该符号在函数 "public: void __thiscall CGetLANAllIpDlg::FindAllComputer(class CStringArray &)" (?FindAllComputer@CGetLANAllIpDlg@@QAEXAAVCStringArray@@@Z) 中被引用rn1>GetLANAllIpDlg.obj : error LNK2019: 无法解析的外部符号 _WNetEnumResourceA@16,该符号在函数 "public: void __thiscall CGetLANAllIpDlg::FindAllComputer(class CStringArray &)" (?FindAllComputer@CGetLANAllIpDlg@@QAEXAAVCStringArray@@@Z) 中被引用rn1>GetLANAllIpDlg.obj : error LNK2019: 无法解析的外部符号 _WNetOpenEnumA@20,该符号在函数 "public: void __thiscall CGetLANAllIpDlg::FindAllComputer(class CStringArray &)" (?FindAllComputer@CGetLANAllIpDlg@@QAEXAAVCStringArray@@@Z) 中被引用rn1>D:\xiaoh\VS2008\GetLANAllIp\Debug\GetLANAllIp.exe : fatal error LNK1120: 3 个无法解析的外部命令rn[/code]rn应该是缺少什么库文件或连接的,我应该怎么弄啊?rn在网上找了一下,说是应该rn#pragma comment( lib, "mpr.dll" )rn但是我加上了之后,会提示无法加载啊……rn问题何在啊??? 论坛

没有更多推荐了,返回首页