同步阻塞通信-BIO编程
来源:互联网 发布:mac地址作用 编辑:程序博客网 时间:2024/06/03 21:18
- 同步阻塞通信模型是传统一请求一应答,即开启一个ServerSocket负责监听socket的连接。
- 服务器实现
package com.haidong.bio;import java.net.ServerSocket;import java.net.Socket;/** * TimeServer * * @author Lance * @date 2017/05/31 */public class TimeServer { public static void main(String[] args) throws Exception { int port = 8080; if(args.length > 0 && args != null){ try { port = Integer.valueOf(port); } catch (NumberFormatException e) { //采用默认值 e.printStackTrace(); } } ServerSocket server = null; try { server = new ServerSocket(port); System.out.println("时间服务器开启:" + port); Socket socket = null; while (true){ //监听 socket = server.accept(); new Thread(new TimeServerHandler(socket)).start(); } } finally { if(server != null){ System.out.println("关闭服务器"); server.close(); server = null; } } }}
3.业务处理线程
package com.haidong.bio;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.util.Date;/** * TimeServerHandler * * @author Lance * @date 2017/05/31 */public class TimeServerHandler implements Runnable { private Socket socket; public TimeServerHandler(Socket socket) { this.socket = socket; } 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("服务器接收到客户端内容:" + body); currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; out.println(currentTime); } } catch (IOException e) { e.printStackTrace(); if(in != null){ try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } in = null; } if(out != null){ out.close(); out = null; } if(this.socket != null){ try { socket.close(); } catch (IOException e1) { e1.printStackTrace(); } socket = null; } } }}
4.客户端实现
package com.haidong.bio;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;/** * TimeClient * * @author Lance * @date 2017/05/31 */public class TimeClient { public static void main(String[] args) { int port = 8080; if(args.length > 0 && args != null){ try { port = Integer.valueOf(port); } finally { //默认值 } } 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("查询时间success"); String resp = in.readLine(); System.out.println("当前时间" + resp); } catch (IOException 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; } } }}
5.运行服务端和客户端结果
阅读全文
0 0
- 同步阻塞通信-BIO编程
- java网络编程(二)----同步阻塞bio
- Socket通信之BIO(同步阻塞IO)、PAIO(伪异步阻塞IO)、NIO(异步非阻塞IO)、AIO(异步非阻塞IO)、netty5之IO
- 同步阻塞式IO(BIO)--学习笔记
- socket实现阻塞式BIO通信
- 同步,异步,阻塞,非阻塞,BIO,AIO,NIO 理解
- 同步阻塞通信-伪异步I/O编程
- java 同步阻塞的BIO、同步非阻塞的NIO、异步非阻塞的AIO
- BIO(同步阻塞)NIO(同步非阻塞)AIO(异步非阻塞)
- 同步阻塞的BIO、同步非阻塞的NIO、异步非阻塞的AIO
- Java IO:面向流、同步、阻塞式IO(BIO)
- [转]彻底理解java aio,bio.nio 同步 异步 阻塞 非阻塞 同步阻塞 同步非阻塞 异步阻塞 异步非阻塞
- 网络编程-阻塞非阻塞同步异步
- 关于同步,异步,阻塞,非阻塞,IOCP/epoll,select/poll,AIO ,NIO ,BIO的总结
- 关于同步,异步,阻塞,非阻塞,IOCP/epoll,select/poll,AIO ,NIO ,BIO的总结
- 关于同步,异步,阻塞,非阻塞,IOCP/epoll,select/poll,AIO ,NIO ,BIO的总结
- 关于同步,异步,阻塞,非阻塞,IOCP/epoll,select/poll,AIO ,NIO ,BIO的总结
- 关于同步和异步,阻塞和非阻塞,BIO和NIO知识点
- 什么叫大数据 大数据的概念
- 南阳理工acm 448寻找最大数(简单贪心)
- Windows安装mongoDB
- 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
- JS 用JS实现判断水仙花数
- 同步阻塞通信-BIO编程
- React之HelloWorld!
- CS231n系列课程Lecture9: Understanding and Visualizing Convolutional Neural Networks
- activity中的label含义
- Linux 设备驱动 ====> 并发控制 --- 自旋锁
- c/c++开发环境
- 如何打开ipynb文件
- Jquery实现鼠标移动/移过到商品小图片上时替换掉大图片上的图片
- 在java中wait和sleep方法的不同?