TCP通信程序练习(服务器数据写入文本文件)

package NetworkProgramming.TCP.SocketDemo03;

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

/**
* FileName: CilentDemo
* Author: lps
* Date: 2022/4/2 12:10
* 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 InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
s.close();

}
}

package NetworkProgramming.TCP.SocketDemo03;

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

/**
* FileName: ServerDemo
* Author: lps
* Date: 2022/4/2 12:11
* Sign:刘品水 Q:1944900433
*/
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1944);
//Socket accept()
//监听要对这个套接字作出的连接并接受它。
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\IdeaProjects\\s.txt"));
String line;
while ((line= br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
System.out.println("数据为:"+line);
}
bw.close();
ss.close();

}
}

 

 

 

 

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