TCP通信程序练习(客户端数据来自于文本文件)

package NetworkProgramming.TCP.SocketDemo04;

import java.io.*;
import java.net.Socket;

/**
* FileName: CilentDemo
* Author: lps
* Date: 2022/4/2 14:13
* Sign:刘品水 Q:1944900433
*/
public class CilentDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.10.102", 1944);
BufferedReader br = new BufferedReader(new FileReader("F:\\IdeaProjects\\bos.java"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}

s.close();
bw.close();
br.close();
}
}

package NetworkProgramming.TCP.SocketDemo04;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
* FileName: ServerDemo
* Author: lps
* Date: 2022/4/2 14:13
* Sign:刘品水 Q:1944900433
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1944);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\IdeaProjects\\copy.java"));
String line;
while((line= br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
System.out.println("接收到的数据为:"+line);
}

bw.close();
br.close();
ss.close();
s.close();


}
}

 

posted @ 2022-04-02 14:34  刘品水  阅读(32)  评论(0编辑  收藏  举报