Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Java SE 综合讨论区
打印话题 寄给朋友 订阅主题 |
话题被移动 | ||||
该话题已被移动 - littledeer1974 , 2004-10-29 13:34 如果您尚不清楚该话题被移动的原因,请参考论坛规则以及本版公告或者联系本版版主。 |
作者 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) [Re:cqlgc] |
alin_ass
发贴: 183 积分: 0 |
于 2003-11-23 20:16
贴以前写的,有个小问题,我不懂jdk的,请问为什么这个类我用起来有时搜索结果,并且返回没有结果的时间太长了,怎么设置超时就算无效ip,请高手支援 /* ------------------------------------------------------------------ * Copyright: Free software. * Author: Alin&ASS(AlinSoftStudio) * Email: alin_ASS@hotmail.com * Time: 12:33:00 2003-11-9 * Project:alinjava * Readme: If you make any modification or have some advice * about this application, I am very glad to know about it. * * Description: Gain a list of machinenames to specified * address in LAN(local area network). The result may not be * complete. I guess simplicity it it becuase of the thread * which use the java.net.InetAddress.getByAddress(byte[]) * will blocked in the Scoket(the more threads the worse)so * as to lose some machinenames. * ------------------------------------------------------------------ */ import java.net.*; /** * Gain a list of machinenames between * address 192.168.0.x~192.168.0.y. * (1<=x<y<=255) * here x and y is specified in main() * method's arguments. * In winOS blank and dot can't be used * in a machinename. * Notice, for example, number 192 in law * IP is -64( it is just <code>(byte)192</code>. * Because law IP use byte numbers. */ public class GainMacNamesInLAN { private int startIP4th = 1; private int endIP4th = 255; private int macNums = 0; private int IP4th = 1; private int sleepedExplorerNums = 0; private int explorerNums = 0; private long startExplorerTime = 0L; private long endExplorerTime = 0L; private class PrintEndStatus extends Thread { private boolean runFlag = false; public void run() { while (true) { if (runFlag) { if (sleepedExplorerNums == explorerNums) { endExplorerTime = System.currentTimeMillis(); printEndInfo(); runFlag = false; System.exit(1); } } else { sleep(); } } } public void start() { if (!runFlag) { runFlag = true; super.start(); } } public void sleep() { try { Thread.sleep(100000); } catch (InterruptedException e) { } } } // end class private class IPExploreThread extends Thread { private boolean runFlag = false; private String hostname = null; private int nowIP4th = 1; public void run() { while (true) { if (runFlag) { nowIP4th = getNextIP4th(); if (nowIP4th == 0) { runFlag = false; addSleepExplorerNums(); } else { try { hostname = InetAddress .getByAddress( new byte[] { -64, -88, 0, (byte) nowIP4th }) .getHostName(); } catch (UnknownHostException e) { } if (hostname.length() > 10 && hostname.substring(0, 4).equals("192.")) { } else { System.out.println( hostname + " / 192.168.0." + nowIP4th); addMacNums(); } } } else { sleep(); } } } public void start() { if (!runFlag) { runFlag = true; super.start(); } } public void sleep() { try { Thread.sleep(1000000); } catch (InterruptedException e) { } } } //end class private void addMacNums() { macNums++; } private void addSleepExplorerNums() { sleepedExplorerNums++; } private void printStartInfo() { System.out.println( "\n\n********** Welcome to use IP Explorer version1.0 ***********"); System.out.println( "Start exploring, using " + explorerNums + " explorers...\n"); } private void printEndInfo() { System.out.println( "\nIP exploring process is finished.\nTotal " + macNums + " machines between 192.168.0." + startIP4th + " to 192.168.0." + endIP4th + " ."); System.out.println( "Waste time: " + (endExplorerTime - startExplorerTime) / 1000L + " seconds."); System.out.println( "************************* alin&ASS *************************"); } private synchronized int getNextIP4th() { if (IP4th > endIP4th) { return 0; } /我要记住下行这样写是可以的 return IP4th++; } /** * Initialize explorer. * @param startAddress start IP's fourth segment * @param endAddress end IP's fourth segment * @param threadNums explorer nums */ private void startIPExploreThreadBetween( int startAddress, int endAddress, int threadNums) { startIP4th = startAddress; IP4th = startIP4th; endIP4th = endAddress; int IPNums = endIP4th - startIP4th + 1; if (IPNums < threadNums) { explorerNums = IPNums; } else { explorerNums = threadNums; } printStartInfo(); startExplorerTime = System.currentTimeMillis(); for (int i = 0; i < explorerNums; i++) { new IPExploreThread().start(); } new PrintEndStatus().start(); } public static void main(String[] args) { GainMacNamesInLAN alinLAN = new GainMacNamesInLAN(); if (args.length == 3) { alinLAN.startIPExploreThreadBetween( Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2])); } else { alinLAN.startIPExploreThreadBetween(1, 50, 10); } } } ------------------------ >java GainMacNamesInLAN 1 255 20 在我机子上的结果: ********** Welcome to use IP Explorer version1.0 *********** Start exploring, using 20 explorers... SK.mshome.net / 192.168.0.3 hong-7i9pfrfq32.mshome.net / 192.168.0.10 yangcat-1314.mshome.net / 192.168.0.1 ass / 192.168.0.2 hongmo.mshome.net / 192.168.0.34 lubing.mshome.net / 192.168.0.93 legend-qxanppz6.mshome.net / 192.168.0.121 jiangzuen.mshome.net / 192.168.0.129 zhoulian.mshome.net / 192.168.0.140 yangkefeng.mshome.net / 192.168.0.141 supu-comp.mshome.net / 192.168.0.145 fuqianjie.mshome.net / 192.168.0.155 fangbin.mshome.net / 192.168.0.161 jtime.mshome.net / 192.168.0.191 IP exploring process is finished. Total 14 machines between 192.168.0.1 to 192.168.0.255 . Waste time: 124 seconds. ************************* alin&ASS ************************* 关于 异常 。。。。 谢谢指教 |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
4869 | 求助(在局域网内如何获得所有网络计算机的名字和ip) | cqlgc | 34 | 2003-11-22 14:23 |
4539 | Re:求助 | lcwling | 56 | 2003-11-22 21:46 |
5023 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) | alin_ass | 5515 | 2003-11-23 20:16 |
4315 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) | cqlgc | 75 | 2003-11-24 13:12 |
4417 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) | nothing | 65 | 2003-11-24 13:24 |
4782 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) | alin_ass | 145 | 2003-11-24 22:51 |
4721 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) | alin_ass | 60 | 2003-11-24 22:55 |
4917 | Re:求助(在局域网内如何获得所有网络计算机的名字和ip) | alin_ass | 424 | 2003-11-26 19:05 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |