日期:2014-05-18 浏览次数:20712 次
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(&qu