怎么确定两台主机间是否互连
我想如果有两台网络设备他们IP分别是192.168.1.1和192.168.1.58 如何用java确定这两台设备之间是否互连了呢?用什么方法 (注:如果这两台设备之间有通过其他设备连接,可以忽略这两台设备之间的那些其他设备,我只要知道这两台设备是否互连) 急!!
[解决办法]
如果只是想通过JAVA 程序来判断两台机器是否连通的话,可以用下面的测试程序,写的不太好,但可以达到你的目的。
Frame_Server.java
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(); } }}
[解决办法]
如果只是想通过JAVA 程序来判断两台机器是否连通的话,可以用下面的测试程序,写的不太好,但可以达到你的目的。
Frame_Server.java
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();
}
}
}