package cn.itcast_03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
//tcp上传文件,这是服务器端
public class ServerDemo {
public static void main(String[] args) throws IOException {
// 创建服务器端Socket对象
ServerSocket ss = new ServerSocket(9100);
// 创建客户端监听对象,返回Socket对象
Socket s = ss.accept();
// 封装通道内流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
// 封装目录
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
// 复制文件
String line = null;
while ((br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
s.close();
}
}
package cn.itcast_03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
//tcp协议客户端
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket接口
Socket s=new Socket("localhost",9100);
//封装文件目录
BufferedReader br=new BufferedReader(new FileReader("aaa.txt"));
//封装通道内流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
s.close();
}
}
|
|