c3 package net; import java.io.*; import java.net.*; public class Cilent { public static void main(String[] args){ String s=null; Socket mysocket=null; DataInputStream in=null; DataOutputStream out=null; try{ mysocket=new Socket("127.0.0.1",9999); in=new DataInputStream(mysocket.getInputStream()); out=new DataOutputStream(mysocket.getOutputStream()); for(int k=1;k<100;k++){ out.writeUTF(""+k); s=in.readUTF(); System.out.println(s); } } catch(Exception e1){ System.out.print("断开"); } } } s3 package net; import java.io.*; import java.net.*; public class Server { public static void main(String[] args) { ServerSocket server=null; Socket you=null; String s=null; DataOutputStream out=null; DataInputStream in=null; try{ server=new ServerSocket(9999); } catch(Exception se){ } try{ System.out.println("等待客户的呼叫"); you=server.accept(); in=new DataInputStream(you.getInputStream()); out=new DataOutputStream(you.getOutputStream()); while(true){ s=in.readUTF(); File file=new File(s); 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(Exception eee){ System.out.println("lost cilent"); } } }