javanio1----传统io
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//每个线程去独自完成他们的客户端请求
public class MyServer {
private static ExecutorService executorService = Executors.newCachedThreadPool();
private static class HandleMsg implements Runnable{
Socket client;
public HandleMsg(Socket client){
this.client = client;
}
@Override
public void run() {
BufferedReader bufferedReader = null;
PrintWriter printWriter = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
printWriter = new PrintWriter(client.getOutputStream(),true); //true是随时刷新
String inputLine = null;
long a = System.currentTimeMillis();
while ((inputLine = bufferedReader.readLine())!=null){// 卡住,等待客户输入,一直等待,直到客户端printWriter输出流关闭,
printWriter.println(inputLine+"server:");
}
long b = System.currentTimeMillis();
System.out.println("此线程花费了:"+(b-a)+"秒!");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedReader.close();
printWriter.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8686);
Socket client = null;
while (true){
client = server.accept(); //服务端监听到一个客户端请求,卡住,等待连接,
System.out.println(client.getRemoteSocketAddress()+"地址的客户端连接成功!");
executorService.submit(new HandleMsg(client));
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket; public class MyClient {
public static void main(String[] args) throws IOException {
Socket client = null;
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {
client = new Socket();
client.connect(new InetSocketAddress("localhost",8686));
printWriter = new PrintWriter(client.getOutputStream(),true);
printWriter.println("hello");
printWriter.flush(); bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream())); //读取服务器返回的信息并进行输出
System.out.println("来自服务器的信息是:"+bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}finally {
printWriter.close();
bufferedReader.close();
client.close();
}
}
}
javanio1----传统io的更多相关文章
- 传统IO与NIO区别二
nio是new io的简称,从jdk1.4就被引入了.现在的jdk已经到了1.6了,可以说不是什么新东西了.但其中的一些思想值得我来研究.这两天,我研究了下其中的套接字部分,有一些心得,在此分享. ...
- 传统IO与NIO(channel-to-channel)文件拷贝的探索与性能比对
Channel-to-channel传输是可以极其快速的,特别是在底层操作系统提供本地支持的时候.某些操作系统可以不必通过用户空间传递数据而进行直接的数据传输.对于大量的数据传输,这会是一个巨大的帮助 ...
- Java传统IO流和NIO流的简单对比介绍
通过对文件的拷贝来对比传统IO流和NIO流 将底层流封装成处理流以后进行分段读取. /*将本身源代码拷贝到TXT文件*/ public class TraditionIO { public stati ...
- 传统IO拷贝与零拷贝技术比较
1. 传统IO 由上面图知,传统io需要经过4次copy, 3次状态切换 第一次: 从硬盘 经过 DMA 拷贝 到 kernel buffer (内核buferr) 第二次: 从kernel buff ...
- 聊聊 传统IO和网络IO
IO 模型 传统 IO读写 磁盘IO主要的延时是由(以15000rpm硬盘为例): 机械转动延时(机械磁盘的主要性能瓶颈,平均为2ms) + 寻址延时(2~3ms) + 块传输延时(一般 ...
- reactor模式前序:传统IO的WEB服务器设计
先看一段经典的WEB JAVA服务器设计 JAVA代码为(伪代码) 1 ServerSocket serverSocket = ...; 2 serverSocket.bind(8899); 3 4 ...
- NIO与传统IO的区别
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
- 传统IO与NIO的比较
本文并非Java.io或Java.nio的使用手册,也不是如何使用Java.io与Java.nio的技术文档.这里只是尝试比较这两个包,用最简单的方式突出它们的区别和各自的特性.Java.nio提出了 ...
- NIO与传统IO的区别<转>
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
- NIO与传统IO的区别(形象比喻)[转]
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
随机推荐
- SegmentedControlIOS使用
代码: import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, SegmentedContr ...
- 报错解决——Disconnected:No supported authentication methods available
该问题是由于ssh链接未允许远程密码认证导致的 解决方法 通过管理控制台进入系统,查看 /etc/ssh/sshd_config配置文件中是否包含以下配置 PasswordAuthentication ...
- Python3学习之路~5.8 shelve模块
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve import datetime name = [& ...
- mac 安装yarn失败
转载:https://www.jianshu.com/p/d4298239e1e4, yarn 下载一些包的时候总是报错,在控制台执行下面的语句后可以下载,具体原因尚未清楚待研究 git config ...
- python的目录
1.python的当前目录 d = os.path.dirname(__file__) #和文件强依赖,即使该语句被别的文件调用,d也不会改变或者d = os.getcwd() #当该语句被别的文件调 ...
- golang 与 c语言 之间传递指针的规则提案
https://go.googlesource.com/proposal/+/master/design/12416-cgo-pointers.md https://github.com/golang ...
- poj3259: Wormholes(BF模板题)
http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...
- [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- ASP.NET 预编译命令(解决发布后第一次访问慢问题)
ASP.NET 编译工具 (Aspnet_compiler.exe) 官方说明 新建bat文件 @echo off CD /d C:\Windows\Microsoft.NET\Framewo ...
- 从现在开始,这是一个IT博客。
从现在开始,删除以前的点点滴滴,一心做个IT人.