IO之BIO

1、BIO工作方式

   服务端监听一个固定的端口,每个客户端连接进来都创建一个线程,并以同步阻塞的方式工作

2、服务端代码

package com.guosen.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * socket服务端
 * @author guosen
 *
 */
public class SocketServer {

	public static void main(String[] args) {
		BufferedReader br = null;
		ServerSocket socket = null;
		Socket s = null;
		OutputStream os = null;
		try {
			// 创建ServerSocket对象
			socket = new ServerSocket(8080);
			// 监听连接到服务端的请求,并包装成Socket返回
			s = socket.accept();
			// 将socket对象的输入流包装成字符流
			br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String line = null;
			// 如果有输入,则打印到控制台,如果输入的内容为end则结束输入
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				if ("end".equals(line)) {
					break;
				}
			}
			// 获取输出流
			os = s.getOutputStream();
			// 给客户端返回收到流的消息
			os.write("I have received your message".getBytes());
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

3、客户端代码

package com.guosen.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
/**
 * socket客户端
 * @author guosen
 *
 */
public class SocketClient {

	public static void main(String[] args) {
		Socket s = null;
		OutputStream os = null;
		BufferedReader br = null;
		try {
			// 创建socket对象
			s = new Socket("127.0.0.1", 8080);
			// 获取输出流对象
			os = s.getOutputStream();
			// 通过流往服务端写数据
			for (int i = 1; i <= 5; i++) {
				os.write(("the " + i + " request\r\n").getBytes());
			}
			// 通过流给服务端发送结束命令
			os.write("end\r\n".getBytes());
			// 刷新流通道
			os.flush();
			// 获取输入流并包装成字符流
			br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			// 定义字符串变量
			String line = null;
			// 当读取到输入时打印到控制台
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}			
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}			
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值