/* *给服务端发送文件,主要是IO流。 */ import java.io.*; import java.net.*; class send2 { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.33.1",10005);//建立服务 BufferedReader bufr = new BufferedReader(new FileReader("io.java"));//读取IO.JAVA文件 PrintWriter pw = new PrintWriter(s.getOutputStream(),true);//将读到的写入服务端 String line = null; while((line = bufr.readLine())!=null) { pw.println(line); } // pw.println("over");//标记结束位置 s.shutdownOutput(); BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//读取服务端返回的数据 String str = bufin.readLine(); System.out.println(str); bufr.close(); s.close(); } } class rece2 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10005);//建立服务 Socket s = ss.accept();//接收数据 BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//读取接收到的数据 PrintWriter out = new PrintWriter(new FileWriter("io2.txt"),true);//写入到IO.TXT文本 String line = null; while((line = bufin.readLine())!= null)//读一行写入一行 { // if("over".equals(line)) // break; out.println(line); } PrintWriter pw = new PrintWriter(s.getOutputStream(),true); pw.println("上传成功!"); out.close(); ss.close(); s.close(); } }
结果