1.TimeServer.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author
* @Description:
* @Date: Created in 11:26 2018/6/28
* @Modified By:
*/
public class TimeServer {
public static void main(String[] args) throws IOException{
int port = 8888;
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;
}
}
}
}
- TimeServerHandler.java
/**
* @author
* @Description:
* @Date: Created in 11:39 2018/6/28
* @Modified By:
*/
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".equalsIgnoreCase(body)?
new java.util.Date(System.currentTimeMillis()).toString():"BAD ORDER";
out.println(currentTime);
}
}catch (Exception e){
e.printStackTrace();
if(in != null){
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(out != null){
out.close();
out = null;
}
if(this.socket != null){
try {
this.socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
this.socket = null;
}
}
}
}
- TimeClient.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author ${user}
* @Description:
* @Date: Created in 11:51 2018/6/28
* @Modified By:
*/
public class TimeClient {
public static void main(String[] args) {
int port = 8888;
if(args != null && args.length >0){
try{
port = Integer.valueOf(args[0]);
}catch (NumberFormatException 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){
//不需要处理
//e.printStackTrace();
}finally {
if(out != null){
out.close();
out = null;
}
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
socket = null;
}
}
}
}
测试结果:
服务器端:
the time server is start in port :8888
the time server receive order:QUERY TIME ORDER
the time server receive order:QUERY TIME ORDER
the time server receive order:QUERY TIME ORDER
客户端:
send order 2 server succeed.
Now is :Thu Jun 28 11:57:35 CST 2018
BIO的缺点:
同步阻塞式IO主要的问题在于每当有一个新的客户端请求接入时,服务器必须创建一个新的县城处理新接入的客户端链路,一个线程只能处理一个客户端请求。在高性能服务器应用领域,往往需要面向成千上万个客户端的并发链接,这种模型显然无法满足高性能,高并发接入的场景。
为了改进一线程一链接模型,可以通过线程池或者消息队列实现1个或多个线程处理N个客户端的模型,由于它的底层通信机制依然使用同步阻塞IO,所以被称为“伪异步”.