JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [問題]有關ICMP的問題 [精華]
andytn





發文: 58
積分: 0
於 2004-04-07 21:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好
我現在想要寫一個程式可以用ICMP來ping別的機器
但不要用外部呼叫ping.exe來做
我寫了好久都寫不出來,也不知道應該用哪個API
有人可以給我點提示嗎


reply to postreply to post
http://blog.andytn.tw
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2004-04-07 21:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://www.teamlinux.org.uk/saxm/projects/icmp/

koji


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
andytn





發文: 58
積分: 0
於 2004-04-08 14:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝
跑不出來
說我library path有問題QQ
努力測試中


reply to postreply to post
http://blog.andytn.tw
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
ray_linn

什么都不懂的小白

版主

發文: 540
積分: 10
於 2004-04-08 15:45 user profilesend a private message to usersend email to ray_linnreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Java寫不出ICMP的,因爲不支援RAW Socket, 只能依賴JNI,Java ARP和Java Ping都是如此。

reply to postreply to post
飞翔的候鸟
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
andytn





發文: 58
積分: 0
於 2004-04-22 15:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
set LD_LIBRARY_PATH
上面這個要怎麼設
我一直設不好QQ


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
bor123





發文: 10
積分: 0
於 2004-04-22 17:03 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這個程式是給linux用的

reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
andytn





發文: 58
積分: 0
於 2004-04-22 23:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這樣還有什麼辦法可以做ping嗎

reply to postreply to post
http://blog.andytn.tw
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
KH

跳的好看嗎 ?



發文: 135
積分: 0
於 2004-04-23 00:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
andytn wrote:
這樣還有什麼辦法可以做ping嗎


用個偷吃步, 試試看.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
 
import java.net.*;
 
class Ping {
 
public static void main(String[] args) {
 
BufferedReader in = null;
 
try {
Runtime r = Runtime.getRuntime();
 
String pingCommand = "ping " + args[0] ;
 
Process p = r.exec(pingCommand);
 
if (p == null) {
System.out.println("Could not connect");
}
 
in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
 
String line;
 
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
} catch (IOException io) {
System.err.println(io.toString());
}
}
}


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:KH]
andytn





發文: 58
積分: 0
於 2004-04-23 13:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
KH wrote:
用個偷吃步, 試試看.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
 
import java.net.*;
 
class Ping {
 
public static void main(String[] args) {
 
BufferedReader in = null;
 
try {
Runtime r = Runtime.getRuntime();
 
String pingCommand = "ping " + args[0] ;
 
Process p = r.exec(pingCommand);
 
if (p == null) {
System.out.println("Could not connect");
}
 
in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
 
String line;
 
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
} catch (IOException io) {
System.err.println(io.toString());
}
}
}



這個我試過了
但感覺不太適合
可能的話不要用ping.exe來做
我是希望能做到一段時間就ping某些機器,這樣就知道他是否還活著
要做到綠燈 紅燈這樣。


reply to postreply to post
http://blog.andytn.tw
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
ccli0709

ccli0709



發文: 46
積分: 1
於 2004-05-05 00:56 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
andytn wrote:
這個我試過了
但感覺不太適合
可能的話不要用ping.exe來做
我是希望能做到一段時間就ping某些機器,這樣就知道他是否還活著
要做到綠燈 紅燈這樣。

我直覺想到判斷字串,
在我的WinXP平台上跑KH大大的偷吃步,
發現第二行固定印出Pinging xxx.xxx.xxx.xxx with 32 bytes of data:
第六行固定印出連線資訊,
我修改了程式為
String line = new String();
//while ((line = in.readLine()) != null) {
for(int i=0 ; i<7 ; i++)
line = in.readLine();

if(line.trim().equals("Request timed out."))
System.out.println("不通");
else
System.out.println("通");
//}
in.close();
藉由判斷第六行出現的字串來得知遠端主機是否存活.
希望這個能幫得上忙.


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
ccli0709

ccli0709



發文: 46
積分: 1
於 2004-05-05 01:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
import java.net.*;
 
class Ping {
    public static void main(String[] args) {
        BufferedReader in = null;
 
        try {
            Runtime r = Runtime.getRuntime();
            
    for(int ii = 0 ; ii < 255 ; ii++)
    {
            String pingCommand = "ping xxx.xxx.xxx."+ ii;
            Process p = r.exec(pingCommand);
 
            if (p == null) {
                System.out.println("Could not connect");
            }
            
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
            String line = new String();
            //while ((line = in.readLine()) != null) {
            for(int i=0 ; i<7 ; i++)
                line = in.readLine();
            
            if(line.trim().equals("Request timed out."))
                System.out.println(pingCommand + "不通");
            else
                System.out.println(pingCommand +"通");
            //}       
            in.close();
    }
            
        } catch (IOException io) {
            System.err.println(io.toString());
        }
    }
}
 

由KH大大的偷吃步改的簡易掃埠程式,
哇~想不到有一天我也能在這裡解決別人的問題,
雖然我的方法算是很暴力,
希望不要丟臉就好.


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:ccli0709]
andytn





發文: 58
積分: 0
於 2004-05-11 12:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ccli0709 wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
import java.net.*;
 
class Ping {
    public static void main(String[] args) {
        BufferedReader in = null;
 
        try {
            Runtime r = Runtime.getRuntime();
            
    for(int ii = 0 ; ii < 255 ; ii++)
    {
            String pingCommand = "ping xxx.xxx.xxx."+ ii;
            Process p = r.exec(pingCommand);
 
            if (p == null) {
                System.out.println("Could not connect");
            }
            
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
            String line = new String();
            //while ((line = in.readLine()) != null) {
            for(int i=0 ; i<7 ; i++)
                line = in.readLine();
            
            if(line.trim().equals("Request timed out."))
                System.out.println(pingCommand + "不通");
            else
                System.out.println(pingCommand +"通");
            //}       
            in.close();
    }
            
        } catch (IOException io) {
            System.err.println(io.toString());
        }
    }
}
 

由KH大大的偷吃步改的簡易掃埠程式,
哇~想不到有一天我也能在這裡解決別人的問題,
雖然我的方法算是很暴力,
希望不要丟臉就好.


太感謝了
這樣看來是可行的
謝謝


reply to postreply to post
http://blog.andytn.tw
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
kevinlin1101





發文: 3
積分: 0
於 2005-01-11 14:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟有找到一個C:\j2sdk-1_4_2-doc\docs\guide\nio\example\Ping.java 這支程式該應可以辦得到 ping 的功能。ping 是netbios-ssn 協定(139 port),所以 java Ping 139 hostName,就會有回應!

reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
morem





發文: 15
積分: 0
於 2006-12-04 13:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這個偷吃步的程式很有趣!
謝謝。


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
morem





發文: 15
積分: 0
於 2006-12-04 14:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這個偷吃步的程式很有趣!
謝謝。


reply to postreply to post
作者 Re:[問題]有關ICMP的問題 [Re:andytn]
xc





發文: 3
積分: 0
於 2007-02-05 21:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以用Jpcap

reply to postreply to post
» JWorld@TW »  Java SE 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8