- /*
- 把客户端的一个文件发到服务端,并在服务端将数据存储到一个文件当中
- 客户端:
- 源:文件
- 目的:socket输出流
- 1.建立socket服务
- 2.获取源和目的;
- 3.从源读取数据,并写入到流中(循环)
- 4.关闭资源
- 服务端:
- 源:socket读取流;
- 目的:本地文件;
- 1.建立socket服务,并获取客户端对象;
- 2.获取socket读取流、文件目的;
- 3.读取流数据,并存入文件中;
- */
- import java.io.*;
- import java.net.*;
- class TextClient
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("111.36.187.4",10001);
- File file = new File("IPDemo.java");
- BufferedReader bufr = new BufferedReader(new FileReader(file));
- BufferedWriter bufOut =
- new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- String line = null;
- while((line = bufr.readLine())!=null)
- {
- bufOut.write(line);
- bufOut.newLine();
- bufOut.flush();
- }
-
-
- bufr.close();
- s.close();
- }
- }
- /*
- 服务端:
- 源:socket读取流;
- 目的:本地文件;
- 1.建立socket服务,并获取客户端对象;
- 2.获取socket读取流、文件目的;
- 3.读取流数据,并存入文件中;
- */
- class TcpSer
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10001);
- Socket s =ss.accept();
- BufferedReader bufIn =
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- File file = new File("IPDemo_copy.java");
- BufferedWriter bufw =
- new BufferedWriter(new FileWriter(file));
- String line = null;
- while((line = bufIn.readLine())!=null)
- {
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- ss.close();
- s.close();
- bufw.close();
- }
- }
复制代码 |
|