import java.io.*; import java.net.*; import java.util.*; import javax.swing.* ; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; public class Cilent { Socket socket=null; JFrame f=new JFrame("cilent"); JTextField text=new JTextField("test2.txt"); JButton btn=new JButton("获取文件"); JTextArea textArea =new JTextArea(""); Thread thread; Socket s; DataOutputStream oos; DataInputStream ois; public static void main(String[] args) { new Cilent(); } public Cilent() { f.setLayout(new FlowLayout()); f.add(btn); f.add(text); textArea.setBounds(11, 111, 600, 400); textArea.setColumns(30); textArea.setRows(20); f.add(textArea); try{ btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ thread=new CilentListener(textArea); thread.start(); } catch(Exception ee){ ee.printStackTrace(); } System.out.println("发送"+text.getText()); } }); } catch(Exception e){ e.printStackTrace(); } f.setBounds(1, 1, 800, 600); f.setVisible(true); f.validate(); } } /** * CilentListener * */ class CilentListener extends Thread{ JTextArea textArea; Socket socket; DataInputStream in ; DataOutputStream out ; public CilentListener(JTextArea textArea) { try { socket=new Socket("127.0.0.1",9999); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.textArea=textArea; try{ in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); } catch(Exception lte){ lte.printStackTrace(); } } public void run(){ while(true){ try{ String readl=in.readUTF(); System.out.println(readl); if(readl!=null) textArea.append(readl); } catch(Exception e2){ e2.printStackTrace(); } } } } import java.io.*; import java.net.*; import java.util.*; import javax.swing.* ; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; public class Server { /** * Vars * */ JFrame f=new JFrame(); Button btn=new Button("监听开始"); Socket s=null; InputStream in=null; StringBuffer sb=null; ListenThread thread; Button send=new Button("go"); DataInputStream oin ; DataOutputStream oos ; /** * Methods * */ public static void main(String[] args) { Server s=new Server(); } public Server() { send.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { try{ oos=thread.out; oos.write("服务器消息".getBytes()); oos.flush(); System.out.println("send"); } catch(Exception e3){ e3.printStackTrace(); } } }); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ thread=new ListenThread(); thread.start(); } catch(Exception ade){ ade.printStackTrace(); } btn.setEnabled(false); } }); f.setLayout(new FlowLayout()); f.add(btn); f.add(send); f.setBounds(1, 1, 800, 600); f.setVisible(true); f.validate(); } } class ListenThread extends Thread{ Socket socket=null; DataInputStream in ; DataOutputStream out ; ServerSocket ss=null; /** * ListenThread * */ public ListenThread() { try{ System.out.println("fsa"); ss=new ServerSocket(9999); System.out.println("fsa22222222"); socket=ss.accept(); System.out.println("2"); in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); System.out.println("正在监听..."); } catch(Exception lte){ lte.printStackTrace(); } } /** * run * */ public void run(){ while(true){ try { String str=in.readUTF(); File file=new File(str); if(!file.exists()){ out.write("文件不存在".getBytes()); break; } String result=""; RandomAccessFile raf=new RandomAccessFile(file,"rw"); String str2=null; while((str2=raf.readLine())!=null){ result+=str2; } out.write(result.getBytes()); out.flush(); } catch (IOException e1) { e1.printStackTrace(); } } } }