1.BIO主要的问题在于每当有一个新的客户端请求接入时,服务器必须创建一个新的线程处理新接入的客户端链路,一个线程只可以处理一个客户端连接,在高性能服务器应用领域,往往需要面向成千上万个客户端的并发连接,这种模型显然无法满足高性能,高并发接入的场合
代码例子:
1.server端代码
package com.afan.bio;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 同步阻塞io
* @author Lenovo
*
*/
public class TimeServer {
public static void main(String[] args) throws IOException{
int port = 8080;
if(args != null && args.length > 0){
try {
port = Integer.valueOf(args[0]);
} catch (NumberFormatException e) {
// 采用默认值
}
}
ServerSocket server = null;
try {
server = new ServerSocket(port);
System.out.println("the time server is start in port:" + port);
Socket socket = null;
while (true) {
socket = server.accept();
new Thread(new TimeServerHandler(socket)).start();
}
} finally{
if(server != null){
System.out.println("the time server close");
server.close();
server = null;
}
}
}
}
2.server端处理类
package com.afan.bio;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Date;
public class TimeServerHandler implements Runnable {
private Socket socket;
public TimeServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(),true);
String currentTime = null;
String body = null;
while(true){
body = in.readLine();
if(body == null){
break;
}
System.out.println("the time server receive order :" + body);
currentTime = "Query TIME ORDER".equals(body) ? new Date(System.currentTimeMillis()).toString()
:"BAD ORDER";
out.println(currentTime);
}
} catch (Exception e) {
if(in != null){
try {
in.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if(out != null){
out.close();
out = null;
}
if(this.socket != null){
try {
this.socket.close();
} catch (Exception e2) {
e2.printStackTrace();
}
this.socket = null;
}
}
}
}
3.客户端代码
package com.afan.bio;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TimeClient {
public static void main(String[] args){
int port = 8080;
if(args != null && args.length > 0){
try {
port = Integer.valueOf(args[0]);
} catch (Exception e) {
//采用默认值
}
}
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try {
socket = new Socket("127.0.0.1",port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
out.println("QUERY TIME ORDER");
System.out.println("send order 2 server succeed.");
String resp = in.readLine();
System.out.println("NOW IS :"+ resp);
} catch (Exception e) {
// 不需要处理
}finally{
if(in != null){
try {
in.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if(out != null){
out.close();
out = null;
}
if(socket != null){
try {
socket.close();
} catch (Exception e2) {
e2.printStackTrace();
}
socket = null;
}
}
}
}