socket编程中BufferedWriter与PrintStream输出方式的区别详解

1、两者区别

(1)BufferedWriter

  write()要输出换行标识,要调用flush()刷新缓冲区

(2)PrintStream

   直接调用输出方法即可

2、BufferedWriter代码演示

(1)客户端

   bw.write(xxx);
   bw.newLine();
   bw.flush();

   三条语句必须同时出现

package socket编程练习01;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * 客户端向服务器写入字符串(键盘录入),服务器(多线程)将字符串反转后写会,客户端再次读取反转后的字符串
* @ClassName: Client02
* @Description: TODO(这里用一句话描述这个类的作用)
* @author: zzb
* @date: 2019年3月2日
*
 */
public class Client02 {
	
	public static void main(String[] args) {
		
		BufferedReader brIn = new BufferedReader(new InputStreamReader(System.in));
		Socket socket = null;
		BufferedWriter bw = null;
		BufferedReader br = null;
		try {
			String readLineIn = brIn.readLine();
			socket = new Socket("127.0.0.1", 12345);
			bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			bw.write(readLineIn);
			bw.newLine();
			bw.flush();
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String readLine = br.readLine();
			System.out.println("服务端返回的字符串== " + readLine);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(brIn != null) {
					brIn.close();
					brIn = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(socket != null) {
					socket.close();
					socket = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(bw != null) {
					bw.close();
					bw = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(br != null) {
					br.close();
					br = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

(2)服务端

   bw.write(xxx);
   bw.newLine();
   bw.flush();

   三条语句必须同时出现

package socket编程练习01;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server02 {
	
	public static void main(String[] args) {
		
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(12345);
			while(true) {
				final Socket socket = serverSocket.accept();
				new Thread(){
					public void run() {
						BufferedReader br = null;
						BufferedWriter bw = null;
						try {
							br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
							bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
							String readLine = br.readLine();
							System.out.println("客户端传递过来的字符串== " + readLine);
							readLine = new StringBuilder(readLine).reverse().toString();
							bw.write(readLine);
							bw.newLine();
							bw.flush();
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							try {
								if(br != null) {
									br.close();
									br = null;
								}
							} catch (IOException e) {
								e.printStackTrace();
							}
							try {
								if(bw != null) {
									bw.close();
									bw = null;
								}
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3、PrintStream代码演示

(1)客户端

   直接调用输出方法ps.println(xxx);

package socket编程练习01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/**
 * 客户端向服务器写入字符串(键盘录入),服务器(多线程)将字符串反转后写会,客户端再次读取反转后的字符串
* @ClassName: Client01
* @Description: TODO(这里用一句话描述这个类的作用)
* @author: zzb
* @date: 2019年3月2日
*
 */
public class Client01 {
	
	public static void main(String[] args) {
		
		BufferedReader brIn = new BufferedReader(new InputStreamReader(System.in));
		Socket socket = null;
		PrintStream ps = null;
		BufferedReader br = null;
		try {
			String readLineIn = brIn.readLine();
			socket = new Socket("127.0.0.1", 12345);
			ps = new PrintStream(socket.getOutputStream());
			ps.println(readLineIn);
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String readLine = br.readLine();
			System.out.println("服务端返回的字符串== " + readLine);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(brIn != null) {
					brIn.close();
					brIn = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(socket != null) {
					socket.close();
					socket = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(ps != null) {
					ps.close();
					ps = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if(br != null) {
					br.close();
					br = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

(2)服务端

   直接调用输出方法ps.println(xxx);

package socket编程练习01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {
	
	public static void main(String[] args) {
		
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(12345);
			while(true) {
				final Socket socket = serverSocket.accept();
				new Thread(){
					public void run() {
						BufferedReader br = null;
						PrintStream ps = null;
						try {
							br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
							ps = new PrintStream(socket.getOutputStream());
							String readLine = br.readLine();
							System.out.println("客户端传递过来的字符串== " + readLine);
							readLine = new StringBuilder(readLine).reverse().toString();
							ps.println(readLine);
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							try {
								if(br != null) {
									br.close();
									br = null;
								}
							} catch (IOException e) {
								e.printStackTrace();
							}
							try {
								if(ps != null) {
									ps.close();
									ps = null;
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(java基础,socket)