C2.java package net; import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class C2 { /** * @param args */ public static void main(String[] args) { new ComputerCilent(); } } class ComputerCilent extends Frame implements Runnable,ActionListener{ Button connection,send; TextField inputText,showResult; Socket socket=null; DataInputStream in=null; DataOutputStream out=null; TextArea text=new TextArea(""); Thread thread; public ComputerCilent() { socket=new Socket(); setLayout(new FlowLayout()); connection=new Button("conn"); inputText=new TextField("test2.txt"); connection.addActionListener(this); send=new Button("send"); send.addActionListener(this); thread=new Thread(this); add(connection); add(inputText); add(send); add(text); this.setVisible(true); this.setBounds(111, 111, 800, 600); } public void actionPerformed(ActionEvent e) { if(e.getSource()==connection){ System.out.println("conn"); try{ if(socket.isConnected()){ System.out.println("socket is connected"); } else{ InetAddress address=InetAddress.getByName("127.0.0.1"); InetSocketAddress socketAddress =new InetSocketAddress(address,8080); socket.connect(socketAddress); in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); thread.start(); } } catch(Exception e2){ e2.printStackTrace(); } } if(e.getSource()==send){ String s=inputText.getText(); try{ out.writeUTF(s); } catch(Exception eeee){} } } public void run() { String str=null; while(true){ try{ str=in.readUTF(); text.append(str); } catch(Exception ee){ System.out.print("lost server"); break; } } } } S2.java package net; import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class S2 { /** * main */ public static void main(String[] args) { ServerSocket server=null; ServerThread thread; Socket you=null; while(true){ try { server=new ServerSocket(8080); } catch (IOException e) { System.out.println("Now...listing"); } try{ System.out.println("waiting cilent"); you=server.accept(); } catch(Exception eee){ System.out.println("Now...waiting"); } if(you!=null){ new ServerThread(you).start(); } } } } class ServerThread extends Thread{ /** * Variables * */ Socket socket; DataOutputStream out=null; DataInputStream in=null; String s=null; boolean quesion=false; /** * 构造方法 * */ public ServerThread(Socket you) { socket=you; try{ out=new DataOutputStream(socket.getOutputStream()); in=new DataInputStream(socket.getInputStream()); } catch(Exception e){} } /** * 传说中的--run方法 * */ public void run() { while(true){ try { String str=in.readUTF(); System.out.println("收到请求:"+str); File file=new File(str); if(!file.exists()){ out.writeUTF("文件不存在"); out.flush(); } else{ String result=""; RandomAccessFile raf=new RandomAccessFile(file,"rw"); String str2=null; while((str2=raf.readLine())!=null){ result+=str2; } out.writeUTF(result); out.flush(); } } catch (IOException e1) { e1.printStackTrace(); } } } }