package cn.itcast_01;
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;
public class ServerDemo {
//这是服务器端
public static void main(String[] args) throws IOException {
//创建接收端的socket对象
ServerSocket ss=new ServerSocket(8888);
//创建监听器对象,返回一个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((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
s.close();
bw.close();
}
}
package cn.itcast_01;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/*
*ip:169.254.90.178
* 功能:使用TCP协议写一个可以上传文件的服务器和客户端
*/
public class Test_07 {
//这是客户端
public static void main(String[] args) throws IOException {
//创建发送端socket对象
Socket s=new Socket("169.254.90.178",8888);
//封装文本文件
BufferedReader br=new BufferedReader(new FileReader("aaa.txt"));
//封装通道内流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
s.close();
}
}
|
|