网络编程的基本模型是Client/Server模型,即一端为服务器端,提供位置信息(IP和端口号),另一端为客户端,通过连接操作向服务器端监听的地址发起连接请求,通过三次握手建立连接,如果连接建立成功,双方就可以通过Socket进行通信。
在基于传统同步阻塞模型开发中,ServerSocket负责绑定IP地址,启动监听端口;Socket负责发起连接操作。连接成功后,双方通过输入和输出流进行同步阻塞式通信。
采用BIO通信模型的服务器端,通常由一个独立的Acceptor线程负责监听客户端的连接,它接受到客户端连接请求之后为每个客户端创建一个新的线程进行链路处理,处理完毕之后,通过输出流返回应答给客户端,线程销毁。即典型的请求—应答模型。
接下来直接上代码:
(1)服务器端
package cn.edu.hust.app.bio;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author chenqiang
* 服务器端
*/
public class TimeServer {
public static void main(String[] args) {
int port = 8080;
if (args != null && args.length > 0) {
try {
port = Integer.valueOf(args[0]);
}catch (Exception e) {
// TODO: handle exception
}
}
ServerSocket server = null;
try {
server = new ServerSocket(port);
System.out.println("Th time server is starting in port: " + port);
Socket socket = null;
while (true) {
socket = server.accept();
new Thread(new TimeServerHandle(socket)).run();;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (server != null) {
System.out.println("The time server close");
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
server = null;
}
}
}
}
package cn.edu.hust.app.bio;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;
/**
*
* @author chenqiang
* 服务线程
*/
public class TimeServerHandle implements Runnable{
private Socket socket;
public TimeServerHandle(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
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 Date(System.currentTimeMillis()).toString() : "BAD ORDER";
out.println(currentTime);
}
}catch (Exception e) {
// TODO: handle exception
if (in != null) {
try {
in.close();
}catch (Exception e1) {
// TODO: handle exception
e.printStackTrace();
}
}
if (out != null) {
out.close();
out = null;
}
if(this.socket != null) {
try {
this.socket.close();
}catch (Exception e2) {
// TODO: handle exception
e.printStackTrace();
}
this.socket = null;
}
}
}
}
(2)客户端
package cn.edu.hust.app.bio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @author chenqiang
*
*/
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) {
// TODO: handle exception
}
}
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) {
// TODO: handle exception
}finally {
if (out != null) {
out.close();
out = null;
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
in = null;
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
socket = null;
}
}
}
该模型最大的问题就是缺乏弹性伸缩能力,当客户端并发访问量增加后,服务端的线程个数和客户端并发访问数呈1:1点正比关系,由于线程是Java虚拟机非常宝贵的系统资源,当线程数膨胀之后,系统的性能急剧下降,随着并发访问量的继续增大,系统会发生线程堆栈溢出、创建新线程失败等问题,并最终导致进程宕机或者僵死,不能对外提供服务。在高性能服务器应用领域,往往需要面向成千上万个客户端的并发连接,这种模型显然无法满足高性能、高并发接入的场景。