BIO
第一次写博客
介绍
BIO是同步阻塞性IO,传输数据使用的流会阻塞效率较慢,同时ServerSocket中的accept方法和socket中的getInputStream/read方法会发生阻塞,也就是接受请求和接收数据会阻塞;
如果使用BIO解决高并发问题必须要引入多线程环境;
一个客户端要创建一个线程,当有大量的请求时但不进行数据传输会浪费资源;
客户端代码
package com.sinosoft;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class BIOClientDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
socket = new Socket("127.0.0.1", 8080);
//客户端阻塞没有关系
//向客户端发送数据
outputStream = socket.getOutputStream();
//阻塞服务端接收请求
//String line = scanner.nextLine();
outputStream.write("request content.".getBytes("utf-8"));
outputStream.flush();
socket.shutdownOutput();
//接收客户端返回数据
inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
StringBuilder response = new StringBuilder();
int length;
if ((length = inputStream.read(bytes)) != -1) {
response.append(new String(bytes, 0, length));
}
System.out.println("response:" + response.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端代码
package com.sinosoft;
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 BIOServerDemo{
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(8080);
while (true) {
//接收socket请求 这句话会阻塞
Socket socket = server.accept();
//每当接收到一个请求则创建一个线程
new Thread(new ServerHandler(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static class ServerHandler implements Runnable {
private Socket socket;
public ServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
//要等待服务端发送数据这时会阻塞
//使用流进行接收数据也会阻塞
bufferedReader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
StringBuilder requestStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
requestStr.append(line);
}
System.out.println("request:" + requestStr.toString());
socket.shutdownInput();
//发送数据
bufferedWriter = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
bufferedWriter.write("response content.");
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
可以使用telnet模拟客户端
使用telnet进行连接
点击ctrl+],即可发送数据