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对象
Socket s = new Socket("117.64.225.33",10086);
// 键盘录入数据
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
// 把通道内的流给包装一下
BufferedWriter bufw = new BufferedWriter
(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while((line = bufr.readLine())!=null){
// 键盘录入数据要自定义结束标记
if("886".equals(line)){
break;
}
bufw.write(line);
bufw.newLine();//用高效流一定记得换行
bufw.flush();
}
// 释放资源
//bufw最终关闭的是s.getOutputStream()流,只要s关闭了这个流就不存在了,所以bufw不必关闭
//bufw.close();
//定义了键盘录入结束,此流也不必关闭
//bufr.close();
s.close();
}}
//服务器端
import java.io.BufferedReader;
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 {
// 创建服务器Socket对象
ServerSocket ss = new ServerSocket(10086);
// 监听客户端连接
Socket s = ss.accept();
// 封装装通道内容的流
BufferedReader bufr = new BufferedReader
(new InputStreamReader(s.getInputStream()));
String line = null;
while((line = bufr.readLine())!=null){
System.out.println(line);
}
s.close();
}}