- 浏览: 979985 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Java Nio系列教程;http://www.iteye.com/magazines/132-Java-NIO
Channel接口定义:http://donald-draper.iteye.com/blog/2369111
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
ServerSocketChannel定义:http://donald-draper.iteye.com/blog/2369836
ServerSocketChannelImpl解析:http://donald-draper.iteye.com/blog/2370912
Selector定义:http://donald-draper.iteye.com/blog/2370015
AbstractSelector定义:http://donald-draper.iteye.com/blog/2370138
SelectorImpl分析 :http://donald-draper.iteye.com/blog/2370519
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper):
http://donald-draper.iteye.com/blog/2370811
WindowsSelectorImpl解析二(选择操作,通道注册,通道反注册,选择器关闭等):
http://donald-draper.iteye.com/blog/2370862
SocketChannel接口定义:http://donald-draper.iteye.com/blog/2371218
SocketChannelImpl 解析一(通道连接,发送数据):http://donald-draper.iteye.com/blog/2372364
SocketChannelImpl 解析二(发送数据后续):http://donald-draper.iteye.com/blog/2372548
SocketChannelImpl 解析三(接收数据):http://donald-draper.iteye.com/blog/2372590
SocketChannelImpl 解析四(关闭通道等) :http://donald-draper.iteye.com/blog/2372717
MembershipKey定义:http://donald-draper.iteye.com/blog/2372947
MulticastChanne接口定义:http://donald-draper.iteye.com/blog/2373009
MembershipKeyImpl 简介:http://donald-draper.iteye.com/blog/2373066
DatagramChannel定义:http://donald-draper.iteye.com/blog/2373046
DatagramChannelImpl 解析一(初始化):http://donald-draper.iteye.com/blog/2373245
DatagramChannelImpl 解析二(报文发送与接收):http://donald-draper.iteye.com/blog/2373281
DatagramChannelImpl 解析三(多播):http://donald-draper.iteye.com/blog/2373507
DatagramChannelImpl 解析四(地址绑定,关闭通道等):http://donald-draper.iteye.com/blog/2373519
Java Nio从JDK1.4之后,才加入到JDK中,在JDK1.1之后,1.4之前,网络编程一般用阻塞BIO,NIO为非阻的IO,NIO主要包括Selector,Channel(Tcp:ServerSocket,Socket;
Udp:Datagram,File;Pipe:Sink,Source),Buffer,SelectionKey等相关的概念在网上有很多这里就不说了,今天文章我们用NIO写一个基于TCP的简单Server和Client。TCP在NIO主要基于ServerSocketChannel和ServerChannel,下面来看实例。
服务端:
客户端:
先启动NIOServer,在启动NIOClient,控制台输出:
NIOServer:
=========The Server is start!===========
message come from client:Hello Server!
NIOClient:
===========The Client is start!===========
message come from server:Hello client!
附:测试Buffer
Channel接口定义:http://donald-draper.iteye.com/blog/2369111
AbstractInterruptibleChannel接口定义:http://donald-draper.iteye.com/blog/2369238
SelectableChannel接口定义:http://donald-draper.iteye.com/blog/2369317
SelectionKey定义:http://donald-draper.iteye.com/blog/2369499
SelectorProvider定义:http://donald-draper.iteye.com/blog/2369615
AbstractSelectableChannel定义:http://donald-draper.iteye.com/blog/2369742
NetworkChannel接口定义:http://donald-draper.iteye.com/blog/2369773
ServerSocketChannel定义:http://donald-draper.iteye.com/blog/2369836
ServerSocketChannelImpl解析:http://donald-draper.iteye.com/blog/2370912
Selector定义:http://donald-draper.iteye.com/blog/2370015
AbstractSelector定义:http://donald-draper.iteye.com/blog/2370138
SelectorImpl分析 :http://donald-draper.iteye.com/blog/2370519
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper):
http://donald-draper.iteye.com/blog/2370811
WindowsSelectorImpl解析二(选择操作,通道注册,通道反注册,选择器关闭等):
http://donald-draper.iteye.com/blog/2370862
SocketChannel接口定义:http://donald-draper.iteye.com/blog/2371218
SocketChannelImpl 解析一(通道连接,发送数据):http://donald-draper.iteye.com/blog/2372364
SocketChannelImpl 解析二(发送数据后续):http://donald-draper.iteye.com/blog/2372548
SocketChannelImpl 解析三(接收数据):http://donald-draper.iteye.com/blog/2372590
SocketChannelImpl 解析四(关闭通道等) :http://donald-draper.iteye.com/blog/2372717
MembershipKey定义:http://donald-draper.iteye.com/blog/2372947
MulticastChanne接口定义:http://donald-draper.iteye.com/blog/2373009
MembershipKeyImpl 简介:http://donald-draper.iteye.com/blog/2373066
DatagramChannel定义:http://donald-draper.iteye.com/blog/2373046
DatagramChannelImpl 解析一(初始化):http://donald-draper.iteye.com/blog/2373245
DatagramChannelImpl 解析二(报文发送与接收):http://donald-draper.iteye.com/blog/2373281
DatagramChannelImpl 解析三(多播):http://donald-draper.iteye.com/blog/2373507
DatagramChannelImpl 解析四(地址绑定,关闭通道等):http://donald-draper.iteye.com/blog/2373519
Java Nio从JDK1.4之后,才加入到JDK中,在JDK1.1之后,1.4之前,网络编程一般用阻塞BIO,NIO为非阻的IO,NIO主要包括Selector,Channel(Tcp:ServerSocket,Socket;
Udp:Datagram,File;Pipe:Sink,Source),Buffer,SelectionKey等相关的概念在网上有很多这里就不说了,今天文章我们用NIO写一个基于TCP的简单Server和Client。TCP在NIO主要基于ServerSocketChannel和ServerChannel,下面来看实例。
服务端:
package nio.simplesocket; 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.util.Iterator; /** * Sever * @author donald * 2017年4月11日 * 下午9:24:03 */ public class NIOServer { //manager the channel private Selector selector; /** * stat Server * @param args * @throws IOException */ public static void main(String[] args) throws IOException{ NIOServer server = new NIOServer(); server.initServer("192.168.32.126", 10000); server.listen(); } /** * get the ServerSocket and finish some initial work * @param port * @throws IOException */ public void initServer(String host, int port) throws IOException{ //get the ServerSocket ServerSocketChannel serverChannel = ServerSocketChannel.open(); // set no blocking mode serverChannel.configureBlocking(false); //bind the port serverChannel.socket().bind(new InetSocketAddress(host, port)); //get the channel manager this.selector = Selector.open(); //Register the channel to manager and bind the event serverChannel.register(selector,SelectionKey.OP_ACCEPT); } /** * use asking mode to listen the event of selector * @throws IOException */ @SuppressWarnings("rawtypes") public void listen() throws IOException{ System.out.println("=========The Server is start!==========="); while(true){ selector.select(); Iterator ite = this.selector.selectedKeys().iterator(); while(ite.hasNext()){ SelectionKey key = (SelectionKey)ite.next(); ite.remove(); if(key.isAcceptable()){ ServerSocketChannel server = (ServerSocketChannel)key.channel(); SocketChannel channel = server.accept(); channel.configureBlocking(false); channel.write(ByteBuffer.wrap(new String("Hello client!").getBytes())); channel.register(this.selector, SelectionKey.OP_READ); } else if (key.isReadable()) read(key); } } } /** * deal with the message come from the client * @param key * @throws IOException */ public void read(SelectionKey key) throws IOException{ SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buf = ByteBuffer.allocate(100); channel.read(buf); byte[] data = buf.array(); String msg = new String(data).trim(); System.out.println("message come from client:"+msg); } }
客户端:
package nio.simplesocket; 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; /** * Client * @author donald * 2017年4月11日 * 下午9:24:09 */ public class NIOClient { //manager the channel private Selector selector; /** * stat Client * @param args * @throws IOException */ public static void main(String[] args) throws IOException{ NIOClient client = new NIOClient(); client.initClient("192.168.32.126",10000); client.listen(); } /** * get the Socket and finish some initial work * @param ip Server ip * @param port connect Server port * @throws IOException */ public void initClient(String ip,int port) throws IOException{ //get the Socket SocketChannel channel = SocketChannel.open(); // set no blocking mode channel.configureBlocking(false); //connect the Server channel.connect(new InetSocketAddress(ip,port)); //get the channel manager this.selector = Selector.open(); //Register the channel to manager and bind the event channel.register(selector,SelectionKey.OP_CONNECT); } /** * use asking mode to listen the event of selector * @throws IOException */ @SuppressWarnings("rawtypes") public void listen() throws IOException{ System.out.println("===========The Client is start!==========="); while(true){ selector.select(); Iterator ite = this.selector.selectedKeys().iterator(); while(ite.hasNext()){ SelectionKey key = (SelectionKey)ite.next(); ite.remove(); if(key.isConnectable()){ SocketChannel channel = (SocketChannel)key.channel(); //during connecting, finish the connect if(channel.isConnectionPending()){ channel.finishConnect(); } channel.configureBlocking(false); channel.write(ByteBuffer.wrap(new String("Hello Server!").getBytes())); channel.register(this.selector, SelectionKey.OP_READ); } else if (key.isReadable()) read(key); } } } /** * deal with the message come from the server * @param key * @throws IOException */ public void read(SelectionKey key) throws IOException{ SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buf = ByteBuffer.allocate(100); channel.read(buf); byte[] data = buf.array(); String msg = new String(data).trim(); System.out.println("message come from server:"+msg); } }
先启动NIOServer,在启动NIOClient,控制台输出:
NIOServer:
=========The Server is start!===========
message come from client:Hello Server!
NIOClient:
===========The Client is start!===========
message come from server:Hello client!
附:测试Buffer
package nio; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; /** * 协议测试 * @author donald * 2017年4月10日 * 下午9:26:57 */ public class testByteBuffer { public static void main(String[] args) { ByteBuffer[] proctols = null;// proctols = new ByteBuffer[2]; ByteBuffer protocolBuffer = null;//协议编码 protocolBuffer = ByteBuffer.allocate(6); try { System.out.println("ProtocolCode String length:"+new String("300000").getBytes("UTF-8").length); protocolBuffer.put(new String("300000").getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println("ProtocolCode length:"+protocolBuffer.position()); proctols[0] = protocolBuffer; ByteBuffer dataBuffer = null;//操作数 dataBuffer = ByteBuffer.allocate(8); dataBuffer.putInt(15); dataBuffer.putInt(6); System.out.println("data length:"+dataBuffer.position()); proctols[1] = dataBuffer; // protocolBuffer.compact();//针对数据太大,缓冲区一次装不完的情况 protocolBuffer.clear(); try { protocolBuffer.put(new String("300100").getBytes("UTF-8")); System.out.println("ProtocolCode length:"+protocolBuffer.position()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // dataBuffer.compact(); dataBuffer.clear(); dataBuffer.putInt(17); dataBuffer.putInt(8); System.out.println("data length:"+dataBuffer.position()); } }
发表评论
-
文件通道解析二(文件锁,关闭通道)
2017-05-16 23:17 1064文件通道解析一(读写操作,通道数据传输等):http://do ... -
文件通道解析一(读写操作,通道数据传输等)
2017-05-16 10:04 1164Reference定义(PhantomRefere ... -
文件通道创建方式综述
2017-05-15 17:39 1065Reference定义(PhantomReference,Cl ... -
文件读写方式简单综述后续(文件,流构造)
2017-05-14 23:04 1480Java Socket通信实例:http://donald-d ... -
文件读写方式简单综述
2017-05-14 11:13 1135Java Socket通信实例:http://donald-d ... -
FileChanne定义
2017-05-12 23:28 936文件读写方式简单综述:http://donald-draper ... -
SeekableByteChannel接口定义
2017-05-11 08:43 1235ByteChannel,分散聚集通道接口的定义(SocketC ... -
FileChannel示例
2017-05-11 08:37 992前面我们看过socket通道,datagram通道,以管道Pi ... -
PipeImpl解析
2017-05-11 08:41 932ServerSocketChannel定义:http://do ... -
Pipe定义
2017-05-10 09:07 903Channel接口定义:http://donald-drape ... -
NIO-Pipe示例
2017-05-10 08:47 905PipeImpl解析:http://donald-draper ... -
DatagramChannelImpl 解析四(地址绑定,关闭通道等)
2017-05-10 08:27 776DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析三(多播)
2017-05-10 08:20 1893DatagramChannelImpl 解析一(初始化):ht ... -
NIO-UDP实例
2017-05-09 12:32 1585DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析二(报文发送与接收)
2017-05-09 09:03 1405DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析一(初始化)
2017-05-08 21:52 1407Channel接口定义:http://donald-drape ... -
MembershipKeyImpl 简介
2017-05-08 09:11 923MembershipKey定义:http://donald-d ... -
DatagramChannel定义
2017-05-07 23:13 1227Channel接口定义:http://donald-drape ... -
MulticastChanne接口定义
2017-05-07 13:45 1136NetworkChannel接口定义:ht ... -
MembershipKey定义
2017-05-06 16:20 915package java.nio.channels; i ...
相关推荐
**JAVA-NIO程序设计完整实例** Java NIO(New IO)是Java 1.4引入的一个新特性,它为Java提供了非阻塞I/O操作的能力,使得Java在处理I/O时更加高效。NIO与传统的BIO(Blocking I/O)模型相比,其核心在于它允许程序...
在这个“tcp.zip”压缩包中,我们可能找到了关于使用Java实现TCP服务器和利用NIO处理多个客户端连接的相关资料。 TCP(传输控制协议)是互联网协议栈中的核心部分,它保证了数据的可靠传输。在Java中,我们可以使用...
标题 "javafr_SERVER-TCP-IP-SOUS-JAVA___Page.zip_page" 暗示了这是一个关于使用Java实现TCP/IP服务器的项目或教程。描述 "server TCP IP with socket" 进一步确认了这个主题,表明内容可能涉及如何在Java中使用...
本实例主要关注NIO在TCP中的应用,它允许更高效的资源管理和处理多个连接,特别适合高并发场景。我们将探讨以下几个关键知识点: 1. **NIO(Non-blocking Input/Output)**: 与传统的BIO不同,NIO是非阻塞的,这...
本项目提供了一个简单的TCP程序实例,即TCP聊天器,它包括服务端和客户端两部分,允许两个或多个用户通过网络进行实时交流。 **TCP协议基础** TCP是一种面向连接的、可靠的、基于字节流的传输层通信协议。它在数据...
这个"MINA TCP简单通信实例"可能涉及到如何使用MINA来实现基于TCP协议的客户端和服务端之间的通信。 在TCP通信中,MINA提供了异步事件驱动的网络应用编程接口(API),它简化了处理网络I/O操作的复杂性。通过MINA,...
服务器端首先需要创建一个`ServerSocket`实例,指定监听的端口号。例如: ```java ServerSocket serverSocket = new ServerSocket(8080); ``` 这里的8080是服务器监听的端口,客户端需要连接这个端口来建立连接。...
这个实例项目可能是通过修改现有代码实现了一个简单的TCP通信应用,比如一个文件传输工具、聊天程序或者数据交换服务。通过分析和学习这个项目,我们可以深入了解Socket编程的基本原理和实践技巧,这对于开发任何...
在"**NIO学习-Java源代码分享**"这个压缩包中,可能包含了关于NIO的示例代码,包括使用NIO进行文件操作、网络通信的实例,也可能包含Netty或Mina的部分源码,这些都可以作为学习和研究NIO技术的宝贵资源。...
在TCP实例中,我们将涉及到以下几个关键概念: 1. **Acceptor**:这是Mina中的核心组件,用于监听和接受来自客户端的连接请求。当一个连接建立后,Acceptor会创建一个新的Session来管理该连接。 2. **Session**:...
Java NIO,全称为Non-Blocking Input/Output(非阻塞输入/输出),是Java从1.4版本开始引入的一种新的I/O模型,它为Java应用程序提供了更高效的数据传输方式。传统的Java I/O模型(BIO)在处理大量并发连接时效率较...
Netty是一个基于NIO的高性能、异步事件驱动的网络应用程序框架,它极大地简化了网络编程,包括TCP和UDP协议的服务器和客户端应用开发。 在Java NIO中,核心组件包括通道(Channel)、缓冲区(Buffer)和选择器...
在尚硅谷的12讲课程中,这些知识点将通过实例演示和详细解释,让学习者掌握Java NIO的精髓,并能够实际应用到项目开发中,提升系统的性能和并发处理能力。通过系统学习,开发者将更好地理解Java NIO的优势,并能在...
这个“基于Java的实例源码-ftp简易服务器.zip”很可能包含了上述部分或全部实现,通过对源码的阅读和学习,我们可以更深入地理解Java网络编程和FTP协议的细节。同时,它也可以作为一个起点,让我们能够根据实际需求...
- **协议支持**:xSocket可能支持多种网络协议,如TCP、UDP等,方便开发者构建各种网络应用。 - **扩展性**:提供灵活的插件机制,方便添加自定义功能或扩展已有功能。 - **稳定性与安全性**:通常会包含错误处理...
在Java编程领域,Socket是网络通信的基础,它允许两个或多个应用程序通过TCP/IP协议进行数据交换。本篇文章将深入探讨如何使用Java NIO(非阻塞I/O)来实现阻塞多线程通信,这对于高性能服务器端应用尤其重要。我们...
- `ServerSocket`类用于监听客户端的连接请求,创建一个`Socket`实例与客户端建立连接。 - `Socket`类代表客户端和服务器之间的连接,它有两个主要的流:`InputStream`和`OutputStream`,分别用于读取和写入数据。...
也就是说,Netty 是一个基于NIO的客户,服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用。Netty相当简化和流线化了网络应用的编程开发过程,例如,...
7. Java NIO应用实例: - 在聊天服务器案例中,使用选择器能够实现用一个线程监听和处理多个通道中的数据传输,使得程序结构更为简洁。 - 通过注册通道到选择器并监听事件,可以实现高效的网络应用,比如聊天...