码迷,mamicode.com
首页 > 其他好文 > 详细

TCP协议客户端键盘录入数据,服务器端写到文本文件

时间:2017-03-14 21:16:13      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:bre   new   tcp协议   服务   close   div   文本   throws   ast   

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协议客户端键盘录入数据,服务器端写到文本文件

标签:bre   new   tcp协议   服务   close   div   文本   throws   ast   

原文地址:http://www.cnblogs.com/zengjiao/p/6550646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!