TCP协议客户端键盘录入数据,服务器端写到文本文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TCP协议客户端键盘录入数据,服务器端写到文本文件相关的知识,希望对你有一定的参考价值。
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(); } }
以上是关于TCP协议客户端键盘录入数据,服务器端写到文本文件的主要内容,如果未能解决你的问题,请参考以下文章