怎么确定两台主机间是否互连 我想如果有两台网络设备他们IP分别是192.168.1.1和192.168.1.58 如何用java确定这两台设备之间是否互连了呢?用什么方法 (注:如果这两台设备之间有通过其他设备连接,可以忽略这两台设备之间的那些其他设备,我只要知道这两台设备是否互连) 急!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么ping ? 我是要知道两台设备之间是否互连 不是我自己的机子是否可以ping到别人的机子 这点小问题都搞不定??import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class ManagerArgs{ public static void main(String[] args) { try { Process p = Runtime.getRuntime().exec("ping 192.168.1.86"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = br.readLine()) != null) { System.out.println(line); System.out.println("I call 有数据耶!"); } br.close(); } catch (IOException e) { e.printStackTrace(); } } } windows--》开始--》运行输入ping 192.168.1.86结果如果是request time out. 说明没通如果有延时 说明通了 你比我还没事找抽型----这个要是星型布局就好办了,将容器步入中心,照我代码改改就O了,要是别的拓扑就不好整了!!你先吧snmp研究透了在来求助吧!!网康就是这些功能,一年服务费10万,谁没事帮你呀,这么大工程。 补充另个问题 怎么用java获取路由表信息? 有没有什么方法? 就算 ping 不通 ,也会有信息出来,这代码也叫牛? 就算 ping 不通 ,也会有信息出来,这代码也叫牛? snmp有个jar包!!研究吧!! telnet 某个端口就行了,ping是不行的,很多服务器,以及路由器都是不给用ping命令了。 如果只是想通过JAVA 程序来判断两台机器是否连通的话,可以用下面的测试程序,写的不太好,但可以达到你的目的。Frame_Server.javaimport java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;public class Frame_Server extends Frame{ static TextArea t_1=new TextArea(); public static void main(String args[]) { // System.out.println(server.in); Frame_Server fs=new Frame_Server("Frame_Server"); fs.setSize(400,300); WindowClosing WC=new WindowClosing(); fs.addWindowListener(WC); fs.panel(); fs.setVisible(true); server.servers(); // } public Frame_Server(String str) { super(str); } public void panel() { Panel p_1=new Panel(); Label l_1=new Label("对方信息:"); p_1.add("North",l_1); p_1.setBackground(Color.pink); add("North",p_1); t_1.setEditable(false); add("Center",t_1); }}class WindowClosing extends WindowAdapter{ public void windowClosing(WindowEvent e) { System.exit(1); }}class server extends Thread{ OutputStream os=null; InputStream is=null; static byte[] buf=new byte[100]; static String in="haha"; static int len=0; private Socket s; public server(Socket s) { this.s=s; } public void run() { try { os=s.getOutputStream(); is=s.getInputStream(); os.write("消息发送成功".getBytes()); len=is.read(buf); System.out.println(new String(buf,0,len)); in="hehe"; Frame_Server.t_1.append(new String(server.in)); Frame_Server.t_1.append(new String(buf,0,len)); is.close(); os.close(); s.close(); } catch(Exception e) { e.printStackTrace(); } } public static void servers() { try { ServerSocket ss=new ServerSocket(6000); while(true) { Socket s=ss.accept(); char[] buf=new char[100]; new server(s).start(); System.out.println("IP is:"+s.getInetAddress()+s.getPort()); String s_3=new String(buf,0,30); System.out.println(s_3); String s_s="hello you !"; Frame_Server.t_1.append(s_3);//new String(s_3)); } } catch(Exception e) { e.printStackTrace(); } }}Frame_Client.javaimport java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;public class Frame_Client extends Frame{ static TextField t_1=new TextField(10); static TextArea t_2=new TextArea(); public static void main(String args[]) { socket_1.clients("Chat now!"); Frame_Client f=new Frame_Client("Frame_Client"); f.setSize(400,200); f.setLocation(400,0); f.setBackground(Color.pink); f.dispose(); f.addMenu(); FA fa=new FA(); f.addWindowListener(fa); f.setVisible(true); } public Frame_Client(String str) { super(str); } public void addMenu() { MenuBar mb=new MenuBar(); Menu file=new Menu("fiel"); MenuItem st=new MenuItem("start"); st.addActionListener(new MenuEvent()); file.add(st); mb.add(file); setMenuBar(mb); } public void dispose() { Panel p_1=new Panel(); Label l_1=new Label("请输入信息:"); p_1.add(l_1); t_1.setEditable(true); p_1.add(t_1); add("North",p_1); t_2.setEditable(false); add("Center",t_2); } }class FA extends WindowAdapter{ public void windowClosing(WindowEvent e) { System.exit(1); }}class MenuEvent implements ActionListener{ public void actionPerformed(ActionEvent e) { String str_1=Frame_Client.t_1.getText(); socket_1 s_1=new socket_1(); new socket_1().clients(str_1); Frame_Client.t_2.append(new String(s_1.buf,0,s_1.len)); }}class socket_1{ static OutputStream os=null; static InputStream is=null; static byte[] buf=new byte[100]; static int len=0; public static void clients(String str) { try { Socket s=new Socket(InetAddress.getByName("192.168.1.1"),6000); os=s.getOutputStream(); is=s.getInputStream(); len=is.read(buf); System.out.println(new String(buf,0,len)); os.write(str.getBytes()); os.close(); is.close(); s.close(); } catch(Exception e) { e.printStackTrace(); } }}先让①Frame_Server.java在ip是192.168.1.1的机器A上运行,②Frame_Client.java在ip是192.168.1.58的机器B上运行,然后在第二台机器上弹出的窗口中写点字,选"file->start",如果在A机器上的窗口上出现了你输入的字,就说明B连通A了。然后,把Frame_Client.java里的Socket s=new Socket(InetAddress.getByName("192.168.1.1"),6000);改成Socket s=new Socket(InetAddress.getByName("192.168.1.58"),6000);类似上面的做法,验证A连通B就可以了。代码不太好,是大学时写的测试程序,有很多多余的地方,但你也能凑活用吧。不知道你画TOP图跟用Java写连通有什么关系。。 网络设备与网络终端应是两个概念,你通过IP只能得到IP终端关系表,得不到拓扑图的ip与掩码的与\异或运算得到广播与网络地址,再减加1得到主机IP范围以宿主主机为圆心,ARP请求所有IP范围MAC,得到宿主与在线IP的辐射图,并解析HOSTS NAME你要自动拓扑,就需要路由发现、网络管理的支持 可以用scanport小工具检测两台电脑互通。 如果只是想通过JAVA 程序来判断两台机器是否连通的话,可以用下面的测试程序,写的不太好,但可以达到你的目的。 Frame_Server.java Java codeimport java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;public class Frame_Server extends Frame{ static TextArea t_1=new TextArea(); public static void main(String args[]) { // System.out.println(server.in); Frame_Server fs=new Frame_Server("Frame_Server"); fs.setSize(400,300); WindowClosing WC=new WindowClosing(); fs.addWindowListener(WC); fs.panel(); fs.setVisible(true); server.servers(); // } public Frame_Server(String str) { super(str); } public void panel() { Panel p_1=new Panel(); Label l_1=new Label("对方信息:"); p_1.add("North",l_1); p_1.setBackground(Color.pink); add("North",p_1); t_1.setEditable(false); add("Center",t_1); }}class WindowClosing extends WindowAdapter{ public void windowClosing(WindowEvent e) { System.exit(1); }}class server extends Thread{ OutputStream os=null; InputStream is=null; static byte[] buf=new byte[100]; static String in="haha"; static int len=0; private Socket s; public server(Socket s) { this.s=s; } public void run() { try { os=s.getOutputStream(); is=s.getInputStream(); os.write("消息发送成功".getBytes()); len=is.read(buf); System.out.println(new String(buf,0,len)); in="hehe"; Frame_Server.t_1.append(new String(server.in)); Frame_Server.t_1.append(new String(buf,0,len)); is.close(); os.close(); s.close(); } catch(Exception e) { e.printStackTrace(); } } public static void servers() { try { ServerSocket ss=new ServerSocket(6000); while(true) { Socket s=ss.accept(); char[] buf=new char[100]; new server(s).start(); System.out.println("IP is:"+s.getInetAddress()+s.getPort()); String s_3=new String(buf,0,30); System.out.println(s_3); String s_s="hello you !"; Frame_Server.t_1.append(s_3);//new String(s_3)); } } catch(Exception e) { e.printStackTrace(); } }}Frame_Client.java Java codeimport java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;public class Frame_Client extends Frame{ static TextField t_1=new TextField(10); static TextArea t_2=new TextArea(); public static void main(String args[]) { socket_1.clients("Chat now!"); Frame_Client f=new Frame_Client("Frame_Client"); f.setSize(400,200); f.setLocation(400,0); f.setBackground(Color.pink); f.dispose(); f.addMenu(); FA fa=new FA(); f.addWindowListener(fa); f.setVisible(true); } public Frame_Client(String str) { super(str); } public void addMenu() { MenuBar mb=new MenuBar(); Menu file=new Menu("fiel"); MenuItem st=new MenuItem("start"); st.addActionListener(new MenuEvent()); file.add(st); mb.add(file); setMenuBar(mb); } public void dispose() { Panel p_1=new Panel(); Label l_1=new Label("请输入信息:"); p_1.add(l_1); t_1.setEditable(true); p_1.add(t_1); add("North",p_1); t_2.setEditable(false); add("Center",t_2); } }class FA extends WindowAdapter{ public void windowClosing(WindowEvent e) { System.exit(1); }}class MenuEvent implements ActionListener{ public void actionPerformed(ActionEvent e) { String str_1=Frame_Client.t_1.getText(); socket_1 s_1=new socket_1(); new socket_1().clients(str_1); Frame_Client.t_2.append(new String(s_1.buf,0,s_1.len)); }}class socket_1{ static OutputStream os=null; static InputStream is=null; static byte[] buf=new byte[100]; static int len=0; public static void clients(String str) { try { Socket s=new Socket(InetAddress.getByName("192.168.1.1"),6000); os=s.getOutputStream(); is=s.getInputStream(); len=is.read(buf); System.out.println(new String(buf,0,len)); os.write(str.getBytes()); os.close(); is.close(); s.close(); } catch(Exception e) { e.printStackTrace(); } }} lz的意思是大概是你的机器和另外两个机器,你要确定另外两个机器是否互联,用你的机器远程连接上一个机器,ping另外一个机器就可以了,可以用远程桌面连接或者ssh都可以,操作系统不一样,东西也不一样,如果不能远程连接的话,你只能写个代码试试了,就像4楼那样。 比较稳妥的办法是在每台要监控的机器上部署你自己的agent,否则很难做到的,总会有一些情况恶心到你的 不错,用PING命令,也可卖本书看一下,很有用的。 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ManagerArgs{ public static void main(String[] args) { try { Process p = Runtime.getRuntime().exec("ping 192.168.1.86"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = br.readLine()) != null) { System.out.println(line); System.out.println("I call 有数据耶!"); } br.close(); } catch (IOException e) { e.printStackTrace(); } } } 只用过socket 学习学习 在机器192.168.1.1的cmd窗口里查看与192.168.1.58能否互连:ping 192.168.1.58查看与192.168.1.58是否正在互连:netstat -a -n | find "ESTABLISHED" ping你要连接的机器啊,如果没有数据丢失的话就说明连通了。 你ping一下不就知道这两台机子是否已经互连了啊在运行里键入cmd然后在dos里键入 ping 主机名 刚用到js有点小迷茫?弱弱的问一个简单的问题 希望大家 来稍微讨论一下 !谢谢额 正则表达式问题 struts2的保存时间问题 proxool连接池如何在class里配置 中文乱码问题. strut问题 js文件如何调用页面上定义的方法? 那位朋友有维新的JAVA视频培训课程,我用张孝祥老师的全套视频和您换着看 类定义问题. 请问谁知道在VisualAge for java下出现No suitable driver是怎么回事 javaScript与DOM的关系.. oracle 分页排序,ssi
import java.io.IOException;
import java.io.InputStreamReader;
public class ManagerArgs{
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("ping 192.168.1.86");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
System.out.println("I call 有数据耶!");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入ping 192.168.1.86
结果如果是request time out. 说明没通
如果有延时 说明通了
就算 ping 不通 ,也会有信息出来,
这代码也叫牛?
就算 ping 不通 ,也会有信息出来,
这代码也叫牛?
snmp
有个jar包!!研究吧!!
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Frame_Server extends Frame
{
static TextArea t_1=new TextArea();
public static void main(String args[])
{
// System.out.println(server.in);
Frame_Server fs=new Frame_Server("Frame_Server");
fs.setSize(400,300);
WindowClosing WC=new WindowClosing();
fs.addWindowListener(WC);
fs.panel();
fs.setVisible(true);
server.servers();
// }
public Frame_Server(String str)
{
super(str);
}
public void panel()
{
Panel p_1=new Panel();
Label l_1=new Label("对方信息:");
p_1.add("North",l_1);
p_1.setBackground(Color.pink);
add("North",p_1);
t_1.setEditable(false);
add("Center",t_1);
}
}class WindowClosing extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}class server extends Thread
{
OutputStream os=null;
InputStream is=null;
static byte[] buf=new byte[100];
static String in="haha";
static int len=0;
private Socket s;
public server(Socket s)
{
this.s=s;
}
public void run()
{
try
{
os=s.getOutputStream();
is=s.getInputStream();
os.write("消息发送成功".getBytes());
len=is.read(buf);
System.out.println(new String(buf,0,len));
in="hehe";
Frame_Server.t_1.append(new String(server.in));
Frame_Server.t_1.append(new String(buf,0,len));
is.close();
os.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void servers()
{
try
{
ServerSocket ss=new ServerSocket(6000);
while(true)
{
Socket s=ss.accept();
char[] buf=new char[100];
new server(s).start();
System.out.println("IP is:"+s.getInetAddress()+s.getPort());
String s_3=new String(buf,0,30);
System.out.println(s_3);
String s_s="hello you !";
Frame_Server.t_1.append(s_3);//new String(s_3));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}Frame_Client.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Frame_Client extends Frame
{
static TextField t_1=new TextField(10);
static TextArea t_2=new TextArea();
public static void main(String args[])
{
socket_1.clients("Chat now!");
Frame_Client f=new Frame_Client("Frame_Client");
f.setSize(400,200);
f.setLocation(400,0);
f.setBackground(Color.pink);
f.dispose();
f.addMenu();
FA fa=new FA();
f.addWindowListener(fa);
f.setVisible(true);
}
public Frame_Client(String str)
{
super(str);
}
public void addMenu()
{
MenuBar mb=new MenuBar();
Menu file=new Menu("fiel");
MenuItem st=new MenuItem("start");
st.addActionListener(new MenuEvent());
file.add(st);
mb.add(file);
setMenuBar(mb);
}
public void dispose()
{
Panel p_1=new Panel();
Label l_1=new Label("请输入信息:");
p_1.add(l_1);
t_1.setEditable(true);
p_1.add(t_1);
add("North",p_1);
t_2.setEditable(false);
add("Center",t_2);
}
}
class FA extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}class MenuEvent implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str_1=Frame_Client.t_1.getText();
socket_1 s_1=new socket_1();
new socket_1().clients(str_1);
Frame_Client.t_2.append(new String(s_1.buf,0,s_1.len));
}
}class socket_1
{
static OutputStream os=null;
static InputStream is=null;
static byte[] buf=new byte[100];
static int len=0;
public static void clients(String str)
{
try
{
Socket s=new Socket(InetAddress.getByName("192.168.1.1"),6000);
os=s.getOutputStream();
is=s.getInputStream();
len=is.read(buf);
System.out.println(new String(buf,0,len));
os.write(str.getBytes());
os.close();
is.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
先让①Frame_Server.java在ip是192.168.1.1的机器A上运行,②Frame_Client.java在ip是192.168.1.58的机器B上运行,然后在第二台机器上弹出的窗口中写点字,选"file->start",如果在A机器上的窗口上出现了你输入的字,就说明B连通A了。
然后,把Frame_Client.java里的Socket s=new Socket(InetAddress.getByName("192.168.1.1"),6000);改成Socket s=new Socket(InetAddress.getByName("192.168.1.58"),6000);类似上面的做法,验证A连通B就可以了。
代码不太好,是大学时写的测试程序,有很多多余的地方,但你也能凑活用吧。
不知道你画TOP图跟用Java写连通有什么关系。。
ip与掩码的与\异或运算得到广播与网络地址,再减加1得到主机IP范围
以宿主主机为圆心,ARP请求所有IP范围MAC,得到宿主与在线IP的辐射图,并解析HOSTS NAME
你要自动拓扑,就需要路由发现、网络管理的支持
Java code
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Frame_Server extends Frame
{
static TextArea t_1=new TextArea();
public static void main(String args[])
{
// System.out.println(server.in);
Frame_Server fs=new Frame_Server("Frame_Server");
fs.setSize(400,300);
WindowClosing WC=new WindowClosing();
fs.addWindowListener(WC);
fs.panel();
fs.setVisible(true);
server.servers();
// }
public Frame_Server(String str)
{
super(str);
}
public void panel()
{
Panel p_1=new Panel();
Label l_1=new Label("对方信息:");
p_1.add("North",l_1);
p_1.setBackground(Color.pink);
add("North",p_1);
t_1.setEditable(false);
add("Center",t_1);
}
}class WindowClosing extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}class server extends Thread
{
OutputStream os=null;
InputStream is=null;
static byte[] buf=new byte[100];
static String in="haha";
static int len=0;
private Socket s;
public server(Socket s)
{
this.s=s;
}
public void run()
{
try
{
os=s.getOutputStream();
is=s.getInputStream();
os.write("消息发送成功".getBytes());
len=is.read(buf);
System.out.println(new String(buf,0,len));
in="hehe";
Frame_Server.t_1.append(new String(server.in));
Frame_Server.t_1.append(new String(buf,0,len));
is.close();
os.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void servers()
{
try
{
ServerSocket ss=new ServerSocket(6000);
while(true)
{
Socket s=ss.accept();
char[] buf=new char[100];
new server(s).start();
System.out.println("IP is:"+s.getInetAddress()+s.getPort());
String s_3=new String(buf,0,30);
System.out.println(s_3);
String s_s="hello you !";
Frame_Server.t_1.append(s_3);//new String(s_3));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Frame_Client.java
Java code
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Frame_Client extends Frame
{
static TextField t_1=new TextField(10);
static TextArea t_2=new TextArea();
public static void main(String args[])
{
socket_1.clients("Chat now!");
Frame_Client f=new Frame_Client("Frame_Client");
f.setSize(400,200);
f.setLocation(400,0);
f.setBackground(Color.pink);
f.dispose();
f.addMenu();
FA fa=new FA();
f.addWindowListener(fa);
f.setVisible(true);
}
public Frame_Client(String str)
{
super(str);
}
public void addMenu()
{
MenuBar mb=new MenuBar();
Menu file=new Menu("fiel");
MenuItem st=new MenuItem("start");
st.addActionListener(new MenuEvent());
file.add(st);
mb.add(file);
setMenuBar(mb);
}
public void dispose()
{
Panel p_1=new Panel();
Label l_1=new Label("请输入信息:");
p_1.add(l_1);
t_1.setEditable(true);
p_1.add(t_1);
add("North",p_1);
t_2.setEditable(false);
add("Center",t_2);
}
}
class FA extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}class MenuEvent implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str_1=Frame_Client.t_1.getText();
socket_1 s_1=new socket_1();
new socket_1().clients(str_1);
Frame_Client.t_2.append(new String(s_1.buf,0,s_1.len));
}
}class socket_1
{
static OutputStream os=null;
static InputStream is=null;
static byte[] buf=new byte[100];
static int len=0;
public static void clients(String str)
{
try
{
Socket s=new Socket(InetAddress.getByName("192.168.1.1"),6000);
os=s.getOutputStream();
is=s.getInputStream();
len=is.read(buf);
System.out.println(new String(buf,0,len));
os.write(str.getBytes());
os.close();
is.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class ManagerArgs{
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("ping 192.168.1.86");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
System.out.println("I call 有数据耶!");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
只用过socket 学习学习
ping 192.168.1.58查看与192.168.1.58是否正在互连:
netstat -a -n | find "ESTABLISHED"
在运行里键入cmd然后在dos里键入 ping 主机名