网络编程的基本模型是Client/Server模型。也就是两个进程之间进行相互通信,当中服务端提供位置信息(

绑定ip地址和监听port),client通过连接操作向服务端监听的地址发送连接请求,通过三次握手建立连接。

假设连接成功。两方就能够通过socket进行通信。

在基于传统的同步堵塞模型开发中。ServerSocket负责绑定IP地址,启动监听port:Socket负责发起连接请求

操作。操作连接成功后,两方通过输入和输出流进行同步堵塞通信。

以下是经典的时间server代码,分析工作过程:

TimeServer代码:

package com.panther.dong.netty.bio.synchronousblockio;

import java.net.ServerSocket;
import java.net.Socket; /**
* server thread(corresponding to all client thread)
* Created by panther on 15-8-11.
*/
public class TimeServer {
public static void main(String[] args) {
int port = 8080;
if (args != null && args.length > 0) {
port = Integer.valueOf(args[0]);
} 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();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (server != null) {
System.out.println("the time server close");
try {
server.close();
} catch (Exception e) { }
server = null;
}
}
}
}

TimerServer依据传入參数设置监听的port。假设没有入參,使用默认8080port。通过构造函数创建ServerSocket

。假设port合法且没有被占用。服务端监听成功。

程序中通过一个循环来监听client的接入,假设没有client的

接入,则线程堵塞在ServerSocket的accept操作上。启动TimeServer,等待client的接入

TimeServerHandler的代码:

package com.panther.dong.netty.bio.synchronousblockio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date; /**
* listener client socket
* Created by panther on 15-8-11.
*/
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 current = null;
String body = null;
while (true) {
body = in.readLine();
if (body == null) {
break;
}
System.out.println("The time server receive order : " + body);
current = "QUERY TIME ORDER".equalsIgnoreCase(body) ?
new Date(System.currentTimeMillis()).toString() :
"BAD ORDER";
out.println(current);
}
} catch (Exception e) {
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;
}
} }
}

client代码TimeClient:

package com.panther.dong.netty.bio.synchronousblockio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket; /**
* client thread(one thread)
* Created by panther on 15-8-13.
*/
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 (IOException e) { } finally {
if (out != null) {
out.close();
out = null;
}
if (in != null) {
try {
in.close();
} catch (IOException e) { }
in = null;
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) { }
socket = null;
}
}
}
}

执行结果:

先执行TimeServer得到结果:

在执行TimeClient。得到例如以下结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

由上面的图可知道。TimeServer和TimeClient建立连接!

!!

BIO的弊端:

每当一个新的client接入请求时,server必须创建一个新的线程处理新接入的链路。一个线程仅仅能处理一个

client的连接。在高性能server的应用领域,往往须要成千上万个client的并发连接。这样的模型无法满足高性能

、高并发接入的场景。!

。。

BIO介绍完成~~~~~~~

最新文章

  1. offsetwidth/clientwidth的区别
  2. linux读写ntfs
  3. $(document).height()、$("body").height()、$(window).height()区别和联系
  4. Android 2D绘图初步
  5. (转)Ubuntu中让终端对于历史输出的内容保持足够长
  6. poj3206(bfs+最小生成树)
  7. C#动态表达式计算
  8. SpringMVC 国际化
  9. MvcMovieStore实例 教程
  10. ssh proxy配置
  11. 设计模式系列之装饰模式(Decorator Pattern)
  12. CSAPP之阅读笔记-计算机系统漫游(1)
  13. es6的模块化--AMD/CMD/commonJS/ES6
  14. redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error(转)
  15. Latex: 解决 The gutter between columns is x inches wide (on page x), but should be at least 0.2 inches. 问题
  16. Android JNI 传递对象
  17. 构建Jenkins自动化编译管理环境
  18. andorid 列表视图之SimpleAdapter
  19. Flask如何给多个视图函数增加装饰器
  20. VUE中父组件向子组件传递数据 props使用

热门文章

  1. Shiro:初识Shiro及简单尝试
  2. synchronized的实现原理及锁优化
  3. Executors线程池关闭时间计算
  4. C++链接和执行相关错误
  5. OS - 线程和进程的差别
  6. 面向程序猿的设计模式 ——GoF《设计模式》读书总结(壹)抽象工厂&生成器
  7. UVA - 10229 Modular Fibonacci 矩阵快速幂
  8. iOS CoreData介绍和使用(以及一些注意事项)
  9. nyoj--233--Sort it (水题)
  10. linux 下的文件搜索、可执行文件搜索