- 浏览: 269758 次
- 性别:
- 来自: 天津
文章分类
- 全部博客 (183)
- oracle (4)
- informix (1)
- web开发 (6)
- java (49)
- hibernate (1)
- hadoop (1)
- spring (23)
- 非技术 (8)
- ibatis2 (5)
- Linux (6)
- tomcat (14)
- nginx (7)
- dubbo (3)
- myibatis (7)
- webservice 开发 (2)
- mysql (2)
- svn (2)
- redis (7)
- 分布式技术 (17)
- zookeeper (2)
- kafka (2)
- velocity (1)
- maven (7)
- js (1)
- freemarker (1)
- Thymeleaf (3)
- 代码审计 (1)
- ibatis3 (1)
- rabbitmq (1)
最新评论
1.编写服务端
package com.boce.nio.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.util.StringUtils;
public class ServerNio implements Runnable {
//选择器
private Selector selector;
private ServerSocketChannel servChannel;
//设置应答线程池
private ExecutorService exeService = Executors.newFixedThreadPool(ServerConfig.SERVER_WRITE_BOOL);
private boolean stop;
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public ServerNio(int port) {
try {
selector = Selector.open();
servChannel = ServerSocketChannel.open();
servChannel.configureBlocking(false);
InetSocketAddress endpoint = new InetSocketAddress(port);
servChannel.socket().bind(endpoint, 1024);
//接收连接继续事件,表示服务器监听到了客户连接,服务器可以接收这个连接了
servChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (Exception e) {
}
}
@Override
public void run() {
while (!stop) {
try {
System.out.println("---------------start");
selector.select();
Set<SelectionKey> set = selector.selectedKeys();
Iterator<SelectionKey> items = set.iterator();
while (items.hasNext()) {
SelectionKey key = items.next();
items.remove();
try{
handleInput(key);
}catch(Exception ex){
if(null !=key){
key.cancel();
}
if(null != key.channel()){
key.channel().close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleInput(final SelectionKey key) throws IOException{
if(key.isValid()){
//处理新信息
if(key.isAcceptable()){
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel sc = ssc.accept();
if(null !=sc){
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
}
}
//可以读取信息了
if(key.isReadable()){
SocketChannel sc = (SocketChannel)key.channel();
//每次读取请求字节数组
ByteBuffer byteBuffer = ByteBuffer.allocate(ServerConfig.SERVER_PER_READ);
//每次请求信息
byte[] temp = new byte[ServerConfig.SERVER_TOTAL_READ];//
//合并数组的下标
int tempstart =0;
StringBuffer sb = new StringBuffer(256);
int len = -1;
while((len = sc.read(byteBuffer)) >0){
//设置byteBuffer的position 为0,从头开始读取
byteBuffer.flip();
int tLen =byteBuffer.remaining();
byte[] bytes = new byte[tLen];
//数据读入bytes数组
byteBuffer.get(bytes);
int end = tempstart+tLen;
//把bytes复制到temp
System.arraycopy(bytes,0,temp,tempstart,tLen);
tempstart = end;
//清除byteBuffer
byteBuffer.clear();
}
sb.append(new String(temp,"UTF-8"));
System.out.println("read ==>"+sb.toString());
sb.insert(0, "server is back==>");
ServerWriteHandle serverWriteHandle = new ServerWriteHandle(sc, sb.toString());
exeService.execute(serverWriteHandle);
if(len <= 0){
//关闭链端
if(null != key && key.isValid()){
key.cancel();
}
// if(null !=sc && sc.isOpen()){
// sc.close();
// }
}
}
}//isValid
}
private void writeMsg(SocketChannel sc,final String msg) throws IOException{
//模拟后台执行
try {
Random dom = new Random();
int time = dom.nextInt(300);
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!StringUtils.isEmpty(msg)){
byte[] bMsg = msg.getBytes(Charset.forName("UTF-8"));
ByteBuffer byteBuffer = ByteBuffer.allocate(bMsg.length);
byteBuffer.put(bMsg);
byteBuffer.flip();
sc.write(byteBuffer);
}
}
}
package com.boce.nio.server;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Random;
import org.springframework.util.StringUtils;
/**
* 发送应答信息
* @author gjp
*
*/
public class ServerWriteHandle implements Runnable{
private SocketChannel sc;
private String msg ;
public ServerWriteHandle(SocketChannel sc, String msg) {
super();
this.sc = sc;
this.msg = msg;
}
@Override
public void run() {
try {
Random dom = new Random();
int time = dom.nextInt(30);
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!StringUtils.isEmpty(msg)){
byte[] bMsg = msg.getBytes(Charset.forName("UTF-8"));
ByteBuffer byteBuffer = ByteBuffer.allocate(bMsg.length);
byteBuffer.put(bMsg);
byteBuffer.flip();
try {
sc.write(byteBuffer);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.boce.nio.server;
/**
* nio server端通用参数
* @author gjp
*
*/
public class ServerConfig {
//写线程池数量
public static final int SERVER_WRITE_BOOL = 300;
//每次读取字节数
public static final int SERVER_PER_READ = 256;
//一次请求读取总字节数(byte)
public static final int SERVER_TOTAL_READ = 1024;
}
编写启动程序:
package com.boce.nio.server;
import java.util.concurrent.Executors;
public class ServerStart {
public static void main(String[] args) {
ServerNio serverNio = new ServerNio(9999);
Executors.newFixedThreadPool(1).execute(serverNio);
}
}
2.编写客户端:
package com.boce.nio.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class ClientNio implements Runnable {
private String host;
private int port;
private Selector selector;
private SocketChannel socketChannel;
private volatile boolean stop;
private int step;
public ClientNio(String host, int port,int step) {
try {
this.host = host == null ? "127.0.0.1" : host;
this.port = port;
this.selector = Selector.open();
this.socketChannel = SocketChannel.open();
this.socketChannel.configureBlocking(false);
this.step = step;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
try {
doConnect();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
while (!stop) {
try {
System.out.println("---------------start");
selector.select();
Set<SelectionKey> set = selector.selectedKeys();
Iterator<SelectionKey> items = set.iterator();
SelectionKey key = null;
while (items.hasNext()) {
key = items.next();
items.remove();
try {
handleInput(key);
} catch (Exception ex) {
if (null != key) {
key.cancel();
}
if (null != key.channel()) {
key.channel().close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException {
if (key.isValid()) {
SocketChannel sc = (SocketChannel) key.channel();
//如果已经连接
if(key.isConnectable()){
if(sc.finishConnect()){
sc.register(selector, SelectionKey.OP_READ);
System.out.println("----------------handleInput***************doWrite");
doWrite(sc);
}else{
System.out.println("连接失败");
System.exit(1);
}
}
if (key.isReadable()) {
// SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int readLen = sc.read(byteBuffer);
if (readLen > 0) {
byteBuffer.flip();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("read sever back msg :: " + body);
stop = true;
// 关闭链端(如果不关闭,客户端最大连接数16286,之后开始报异常)
if(null !=key){
key.cancel();
}
if(null !=sc){
sc.close();
}
} else if (readLen < 0) {
// 关闭链端
if(null !=key){
key.cancel();
}
if(null !=sc){
sc.close();
}
} else {
System.out.println("读取数据 为0");
;// 读取数据为0;忽略
}
}
} // isValid
}
private void doConnect() throws IOException {
// 如果注册成功,注册到多路选择器,发送信息,读取信息
boolean isConn = socketChannel.connect(new InetSocketAddress(host, port));
if (isConn) {
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("doconnect .....");
doWrite(socketChannel);
} else {
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
private void doWrite(SocketChannel sc) throws IOException {
String send ="requst time 请求===写入客户端信息内容************************************==========================="+step;
System.out.println("发送信息:"+send);
byte[] msg = send.getBytes("UTF-8");
ByteBuffer byteBuffer = ByteBuffer.allocate(msg.length);
byteBuffer.put(msg);
String position1 ="position"+ byteBuffer.position()+";capacity="+byteBuffer.capacity()+
";limit="+byteBuffer.limit()+";mark"+byteBuffer.mark()+
";remain"+byteBuffer.hasRemaining();
System.out.println(position1);
byteBuffer.flip();
String position2 = byteBuffer.position()+";capacity="+byteBuffer.capacity()+
";limit="+byteBuffer.limit()+";mark"+byteBuffer.mark()+
";remain"+byteBuffer.hasRemaining();
System.out.println(position2);
sc.write(byteBuffer);
if (!byteBuffer.hasRemaining()) {
System.out.println(" send order 2 success");
}
}
}
客户端启动程序:
package com.boce.nio.server;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class ClientStart {
public static void main(String[] args) {
String host="127.0.0.1";
int port =9999;
AtomicInteger ac = new AtomicInteger(0);
ExecutorService exe = Executors.newFixedThreadPool(100);
for(int i=0;i<1;i++){
ClientNio client = new ClientNio(host, port,ac.getAndIncrement());
exe.execute(client);
}
exe.shutdown();
}
}
测试发送记录条数:
package com.boce.nio.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.springframework.util.StringUtils;
public class ServerNio implements Runnable {
//选择器
private Selector selector;
private ServerSocketChannel servChannel;
//设置应答线程池
private ExecutorService exeService = Executors.newFixedThreadPool(ServerConfig.SERVER_WRITE_BOOL);
private boolean stop;
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public ServerNio(int port) {
try {
selector = Selector.open();
servChannel = ServerSocketChannel.open();
servChannel.configureBlocking(false);
InetSocketAddress endpoint = new InetSocketAddress(port);
servChannel.socket().bind(endpoint, 1024);
//接收连接继续事件,表示服务器监听到了客户连接,服务器可以接收这个连接了
servChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (Exception e) {
}
}
@Override
public void run() {
while (!stop) {
try {
System.out.println("---------------start");
selector.select();
Set<SelectionKey> set = selector.selectedKeys();
Iterator<SelectionKey> items = set.iterator();
while (items.hasNext()) {
SelectionKey key = items.next();
items.remove();
try{
handleInput(key);
}catch(Exception ex){
if(null !=key){
key.cancel();
}
if(null != key.channel()){
key.channel().close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleInput(final SelectionKey key) throws IOException{
if(key.isValid()){
//处理新信息
if(key.isAcceptable()){
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel sc = ssc.accept();
if(null !=sc){
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
}
}
//可以读取信息了
if(key.isReadable()){
SocketChannel sc = (SocketChannel)key.channel();
//每次读取请求字节数组
ByteBuffer byteBuffer = ByteBuffer.allocate(ServerConfig.SERVER_PER_READ);
//每次请求信息
byte[] temp = new byte[ServerConfig.SERVER_TOTAL_READ];//
//合并数组的下标
int tempstart =0;
StringBuffer sb = new StringBuffer(256);
int len = -1;
while((len = sc.read(byteBuffer)) >0){
//设置byteBuffer的position 为0,从头开始读取
byteBuffer.flip();
int tLen =byteBuffer.remaining();
byte[] bytes = new byte[tLen];
//数据读入bytes数组
byteBuffer.get(bytes);
int end = tempstart+tLen;
//把bytes复制到temp
System.arraycopy(bytes,0,temp,tempstart,tLen);
tempstart = end;
//清除byteBuffer
byteBuffer.clear();
}
sb.append(new String(temp,"UTF-8"));
System.out.println("read ==>"+sb.toString());
sb.insert(0, "server is back==>");
ServerWriteHandle serverWriteHandle = new ServerWriteHandle(sc, sb.toString());
exeService.execute(serverWriteHandle);
if(len <= 0){
//关闭链端
if(null != key && key.isValid()){
key.cancel();
}
// if(null !=sc && sc.isOpen()){
// sc.close();
// }
}
}
}//isValid
}
private void writeMsg(SocketChannel sc,final String msg) throws IOException{
//模拟后台执行
try {
Random dom = new Random();
int time = dom.nextInt(300);
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!StringUtils.isEmpty(msg)){
byte[] bMsg = msg.getBytes(Charset.forName("UTF-8"));
ByteBuffer byteBuffer = ByteBuffer.allocate(bMsg.length);
byteBuffer.put(bMsg);
byteBuffer.flip();
sc.write(byteBuffer);
}
}
}
package com.boce.nio.server;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Random;
import org.springframework.util.StringUtils;
/**
* 发送应答信息
* @author gjp
*
*/
public class ServerWriteHandle implements Runnable{
private SocketChannel sc;
private String msg ;
public ServerWriteHandle(SocketChannel sc, String msg) {
super();
this.sc = sc;
this.msg = msg;
}
@Override
public void run() {
try {
Random dom = new Random();
int time = dom.nextInt(30);
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!StringUtils.isEmpty(msg)){
byte[] bMsg = msg.getBytes(Charset.forName("UTF-8"));
ByteBuffer byteBuffer = ByteBuffer.allocate(bMsg.length);
byteBuffer.put(bMsg);
byteBuffer.flip();
try {
sc.write(byteBuffer);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
sc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.boce.nio.server;
/**
* nio server端通用参数
* @author gjp
*
*/
public class ServerConfig {
//写线程池数量
public static final int SERVER_WRITE_BOOL = 300;
//每次读取字节数
public static final int SERVER_PER_READ = 256;
//一次请求读取总字节数(byte)
public static final int SERVER_TOTAL_READ = 1024;
}
编写启动程序:
package com.boce.nio.server;
import java.util.concurrent.Executors;
public class ServerStart {
public static void main(String[] args) {
ServerNio serverNio = new ServerNio(9999);
Executors.newFixedThreadPool(1).execute(serverNio);
}
}
2.编写客户端:
package com.boce.nio.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class ClientNio implements Runnable {
private String host;
private int port;
private Selector selector;
private SocketChannel socketChannel;
private volatile boolean stop;
private int step;
public ClientNio(String host, int port,int step) {
try {
this.host = host == null ? "127.0.0.1" : host;
this.port = port;
this.selector = Selector.open();
this.socketChannel = SocketChannel.open();
this.socketChannel.configureBlocking(false);
this.step = step;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
try {
doConnect();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
while (!stop) {
try {
System.out.println("---------------start");
selector.select();
Set<SelectionKey> set = selector.selectedKeys();
Iterator<SelectionKey> items = set.iterator();
SelectionKey key = null;
while (items.hasNext()) {
key = items.next();
items.remove();
try {
handleInput(key);
} catch (Exception ex) {
if (null != key) {
key.cancel();
}
if (null != key.channel()) {
key.channel().close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
if (selector != null) {
try {
selector.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException {
if (key.isValid()) {
SocketChannel sc = (SocketChannel) key.channel();
//如果已经连接
if(key.isConnectable()){
if(sc.finishConnect()){
sc.register(selector, SelectionKey.OP_READ);
System.out.println("----------------handleInput***************doWrite");
doWrite(sc);
}else{
System.out.println("连接失败");
System.exit(1);
}
}
if (key.isReadable()) {
// SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int readLen = sc.read(byteBuffer);
if (readLen > 0) {
byteBuffer.flip();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("read sever back msg :: " + body);
stop = true;
// 关闭链端(如果不关闭,客户端最大连接数16286,之后开始报异常)
if(null !=key){
key.cancel();
}
if(null !=sc){
sc.close();
}
} else if (readLen < 0) {
// 关闭链端
if(null !=key){
key.cancel();
}
if(null !=sc){
sc.close();
}
} else {
System.out.println("读取数据 为0");
;// 读取数据为0;忽略
}
}
} // isValid
}
private void doConnect() throws IOException {
// 如果注册成功,注册到多路选择器,发送信息,读取信息
boolean isConn = socketChannel.connect(new InetSocketAddress(host, port));
if (isConn) {
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("doconnect .....");
doWrite(socketChannel);
} else {
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
private void doWrite(SocketChannel sc) throws IOException {
String send ="requst time 请求===写入客户端信息内容************************************==========================="+step;
System.out.println("发送信息:"+send);
byte[] msg = send.getBytes("UTF-8");
ByteBuffer byteBuffer = ByteBuffer.allocate(msg.length);
byteBuffer.put(msg);
String position1 ="position"+ byteBuffer.position()+";capacity="+byteBuffer.capacity()+
";limit="+byteBuffer.limit()+";mark"+byteBuffer.mark()+
";remain"+byteBuffer.hasRemaining();
System.out.println(position1);
byteBuffer.flip();
String position2 = byteBuffer.position()+";capacity="+byteBuffer.capacity()+
";limit="+byteBuffer.limit()+";mark"+byteBuffer.mark()+
";remain"+byteBuffer.hasRemaining();
System.out.println(position2);
sc.write(byteBuffer);
if (!byteBuffer.hasRemaining()) {
System.out.println(" send order 2 success");
}
}
}
客户端启动程序:
package com.boce.nio.server;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class ClientStart {
public static void main(String[] args) {
String host="127.0.0.1";
int port =9999;
AtomicInteger ac = new AtomicInteger(0);
ExecutorService exe = Executors.newFixedThreadPool(100);
for(int i=0;i<1;i++){
ClientNio client = new ClientNio(host, port,ac.getAndIncrement());
exe.execute(client);
}
exe.shutdown();
}
}
测试发送记录条数:
发表评论
-
aop实现通用缓存,并且防止缓存击穿
2019-09-16 15:10 798实现代码在附件中 1.自定义注解文件 package sgn ... -
统计多线程下程序运行总时间
2019-05-15 16:55 1098package com.gpcsoft.hct.epp.egp ... -
通过模板的方式解决缓存被击穿的问题
2019-04-15 11:35 4091. package gjp.tools; import c ... -
Rsa 加解密算法
2019-03-18 10:27 368package gjp.tools; /** * @Aut ... -
httpClient 使用http协议上传文件
2018-10-09 15:58 3102<dependency> <grou ... -
httpClient 使用HTTPS 协议上传文件
2018-09-30 14:50 2436<dependency> <group ... -
防止 XML外部实体注入
2018-09-18 17:03 7308方式一 DocumentBuilderFactory dbf ... -
httpClient 的https 调用
2018-06-20 21:07 808package com.gpcsoft.xjmodule.ut ... -
猎狗方式调用接口
2017-09-27 08:36 647package boce.hit.dog; import j ... -
netty 实现长连接
2017-08-24 09:52 13091.server 端信息 package com.boce.n ... -
netty 开发入门
2017-08-22 14:30 6681.准备jar <properties> & ... -
jwt 生成token 和解析token
2017-06-06 16:45 5915<jjwt.version>0.6.0</j ... -
实现Java高并发隔离 模拟
2017-05-08 10:34 513package org; import java.util. ... -
java 命令
2017-04-20 16:42 417java 命令: java -Djava.ext.dirs ... -
nio 通讯
2017-04-01 15:41 525nio 服务端: package nio.study.se ... -
HashMap 便利时不按照输入顺序输出
2017-03-27 17:11 1954使用:hashmap传输数据时,便利map中的数据时,发现 ... -
使用Lock,对不同商品加锁
2017-03-13 10:52 1247package com.boce.gbkutf; ... -
json 转泛型的集合类
2017-03-07 16:21 1213package com.boce.test; ... -
httpclient4.5 使用post方式提交请求
2017-03-03 11:00 2014private RequestConfig req ... -
GBK与UTF-8 字符串互转
2017-02-24 11:17 2206package com.cloud.tools; i ...
相关推荐
本实例将深入探讨NIO的基本概念、关键组件以及如何在实际编程中应用NIO。 ### 1. NIO基本概念 - **通道(Channel)**: 通道是NIO的核心概念,它类似于流,但可以双向传输数据。常见的通道类有FileChannel、...
4. **NIO编程流程**: - 打开通道:根据需求创建FileChannel、SocketChannel或ServerSocketChannel。 - 创建缓冲区:根据需要处理的数据类型创建对应的Buffer实例。 - 读写数据:使用通道与缓冲区交互,将数据从...
标题中的"NIO实例"指的是Java中的非阻塞I/O(Non-blocking Input/Output)技术,它是Java从1.4版本开始引入的一种I/O模型,旨在提高网络编程的效率和性能。与传统的IO(BIO)相比,NIO具有异步、非阻塞的特点,能够...
本书《Java网络编程实例:Java网络编程实例》显然聚焦于通过实际案例来教授这一核心技能。以下是一些主要的知识点,这些知识点通常会在书中详细讨论: 1. **TCP/IP协议基础**:首先,了解TCP/IP模型和协议栈是非常...
标题“nio.rar_NIO_NIO-socket_java nio_java 实例_java.nio”表明这个压缩包包含了一个关于Java NIO的实例,特别是关于NIO套接字(Socket)的编程示例。NIO套接字是Java NIO库中用于网络通信的关键组件,它们允许...
本资料"JavaNIO服务器实例Java开发Java经验技巧共6页"可能是某个Java开发者或讲师分享的一份关于如何在Java中构建NIO服务器的教程,涵盖了6个关键页面的内容。尽管具体的细节无法在此直接提供,但我们可以根据Java ...
NIO不仅适用于网络编程,如服务器端的高并发处理,还广泛应用于文件系统操作、数据库连接池等场景。在大型分布式系统中,NIO可以帮助优化性能,提高系统的响应速度。 通过学习和掌握Java NIO,开发者可以构建出更加...
Java基础编程实例是Java初学者入门的绝佳资源,它涵盖了Java语言的核心概念和技术。这个教程旨在帮助新手理解并掌握Java编程的基本元素,从而能够独立编写简单的程序。以下是一些主要的知识点: 1. **Java环境配置*...
这个"java网络编程实例.rar"的压缩包显然包含了帮助开发者理解和实践Java网络编程的资源。让我们详细探讨一下Java网络编程的核心概念和相关知识点。 首先,Java网络编程主要基于Java的Socket编程模型,它允许程序...
这个"java网络编程实例2"很可能包含了深入的实践案例,帮助读者理解并掌握网络编程的基本概念和技术。以下是根据标题和描述可能涵盖的一些关键知识点: 1. **网络基础知识**:在Java中进行网络编程首先需要了解TCP/...
### Java NIO网络编程核心知识点解析 #### 非阻塞式Socket通信:Java NIO的革命性突破 从JDK 1.4版本开始,Java引入了NIO(Non-blocking I/O)API,这标志着Java网络编程的一个重大转折点。传统上,基于阻塞I/O的...
Java编程实例是学习和深化Java语言技能的重要途径。这些实例涵盖了基础到高级的各种主题,旨在帮助开发者理解并熟练掌握Java编程。以下是一些基于提供的压缩包文件“Java编程100例”可能涵盖的关键知识点: 1. **...
客户端则会创建Bootstrap实例,设置通道工厂,通常是NioSocketChannel。同样地,客户端也需要配置管道,其中包含一个ByteToMessageDecoder和一个ClientBusinessHandler,后者负责与服务器交互的逻辑。 在启动服务器...
在这个实例中,我们将深入探讨Java网络编程的基础及其在实际应用中的实践。本文将覆盖以下几个关键知识点: 1. **Java网络编程基础** - Java提供了丰富的类库支持网络编程,如`java.net`包下的Socket、...
本实例将深入探讨NIO在服务器端和客户端的应用,通过`NioDemoServer.java`和`NioDemoClient.java`这两个文件来解析NIO的核心知识点。 1. **NIO基础** NIO的核心组件包括通道(Channels)、缓冲区(Buffers)和选择...
本实例"socket通信NIO代理模式demo"将展示如何利用NIO来构建一个高性能的代理服务器。 代理模式是一种设计模式,它允许我们创建一个代理对象来控制对原对象的访问。在NIO代理模式中,代理服务器作为客户端与目标...
Java网络编程是Java开发中的重要领域,它涵盖了网络应用...提供的《Java网络编程实例》将可能包含这些概念的具体代码示例,帮助读者更好地理解和掌握相关技术。阅读和分析这些实例,是提升Java网络编程技能的有效途径。
在"Java网络编程实例1"这个压缩包中,可能包含了实际的Java代码示例,包括简单的客户端-服务器通信,多线程服务器,使用URL和URLConnection下载文件,以及可能的NIO实现。这些实例可以帮助读者更直观地理解上述理论...
本实例将深入探讨Netty如何实现NIO在高并发场景下的优化和应用。 首先,让我们理解Java的NIO(Non-blocking I/O,非阻塞I/O)。传统的Java IO模型(BIO)是基于流(Stream)的,对每一个连接都需要一个线程来处理,当...