package cn.idcast9; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; // 客户端键盘录入数据,服务器端写到文本文件 public class ClientDemo { public static void main(String[] args) throws IOException { Socket s = new Socket("192.168.1.115", 12345); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { if ("over".equals(line)) { break; } bw.write(line); bw.newLine(); bw.flush(); } s.close(); } }
package cn.idcast9; 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 { ServerSocket ss = new ServerSocket(12345); Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader( s.getInputStream())); //服务器端写到文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); s.close(); } }
时间: 2024-11-18 21:08:40