/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mailclass;
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.Date;
import java.util.Iterator;
import java.util.Set;
/**
*
* @author AAries zyz!
*/
public class MainClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
ServerSocketChannel server = ServerSocketChannel.open();
server.configureBlocking(false);
server.socket().bind(new InetSocketAddress(5200));
Selector select = Selector.open();
server.register(select, SelectionKey.OP_ACCEPT);
while (true) {
if (select.select(10000) == 0) {
System.out.print("独自等待.");
continue;
}
select.select();
Set readkeys = select.selectedKeys();
Iterator iterator = readkeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = (SelectionKey) iterator.next();
if (key.isAcceptable()) {
SocketChannel client = ((ServerSocketChannel) key.channel()).accept();
System.out.println("Accept connection from: " + client);
client.configureBlocking(false);
client.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocate(1024));
}
if (key.isWritable()) {
System.out.print("Write data...");
}
if (key.isReadable()) {
// 获得与客户端通信的信道
SocketChannel clientChannel = (SocketChannel) key.channel();
// 得到并清空缓冲区
ByteBuffer buffer = (ByteBuffer) key.attachment();
buffer.clear();
// 读取信息获得读取的字节数
long bytesRead = clientChannel.read(buffer);
if (bytesRead == -1) {
// 没有读取到内容的情况
clientChannel.close();
} else {
// 将缓冲区准备为数据传出状态
buffer.flip();
// 将字节转化为为UTF-16的字符串
String receivedString = Charset.forName("UTF-16").newDecoder().decode(buffer).toString();
// 控制台打印出来
System.out.println("接收到来自" + clientChannel.socket().getRemoteSocketAddress() + "的信息:" + receivedString);
// 准备发送的文本
String sendString = "你好,客户端. @" + new Date().toString() + ",已经收到你的信息" + receivedString;
buffer = ByteBuffer.wrap(sendString.getBytes("UTF-16"));
clientChannel.write(buffer);
// 设置为下一次读取或是写入做准备
key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
}
if (key.isWritable())
{
SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer writeBuffer=ByteBuffer.wrap("我的程序员之道".getBytes("UTF-16"));
sc.write(writeBuffer);
}
key.channel().close();
}
}
}
}
输出结果:
server端
run:
Accept connection from: java.nio.channels.SocketChannel[connected local=/172.16.22.11:5200 remote=/172.16.22.11:4546]
接收到来自/172.16.22.11:4546的信息:我的程序员之道
独自等待.独自等待.独自等待.独自等待.独自等待.独自等待.独自等待.独自等待.独自等待.独自等待.生成已停止(总时间:1 分钟 49 秒)
client端:
接收到来自服务器/172.16.22.11:5200的信息:你好,客户端. @Wed Aug 03 18:55:42 CST 2011,已经收到你的信息我的程序员之道
接收到来自服务器/172.16.22.11:5200的信息:
接收到来自服务器/172.16.22.11:5200的信息:
接收到来自服务器/172.16.22.11:5200的信息:
分享到:
相关推荐
在Java NIO(非阻塞I/O)框架中,SocketChannel、ServerSocketChannel和Selector扮演着关键角色。本文将深入探讨这些组件,并通过一个实际案例——"NServer.java"和"NClient.java"来阐述它们的工作原理和用法。 ...
为了解决这个问题,Java引入了非阻塞I/O(Non-blocking I/O,简称NIO),其中的核心类包括ServerSocketChannel和SocketChannel。本篇文章将详细解析如何使用非阻塞ServerSocketChannel和SocketChannel来替代传统的...
Java的`ServerSocketChannel`和`SocketChannel`是NIO(非阻塞I/O)框架中的核心组件,它们为创建高性能、高并发的网络服务提供了基础。在Java中,传统的I/O模型基于流(Stream),而NIO则引入了通道(Channel)和...
通过调用`ServerSocketChannel.open()`方法可以创建一个ServerSocketChannel对象,然后通过`bind()`方法绑定到指定的IP和端口上。当有客户端连接请求时,`accept()`方法会返回一个新的SocketChannel,用于处理客户端...
java api之ServerSocketChannel基础、应用场景、实战讲解
jdk api-ServerSocketChannel、Selector、ByteBuffer结合实现网络报文间的通讯
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 设置为非阻塞模式 serverSocketChannel.configureBlocking(false); // 绑定端口 serverSocketChannel.bind(new InetSocketAddress...
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); ``` 2. **绑定端口**:服务器需要在特定端口上监听连接,可以调用`serverSocketChannel.bind(new InetSocketAddress(port))`方法进行绑定...
● ServerSocketChannel:ServerSocket的替代类,支持阻塞通信与非阻塞通信。 ● SocketChannel:Socket的替代类,支持阻塞通信与非阻塞通信。 ● Selector:为ServerSocketChannel监控接收连接就绪事件,为...
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.bind(new InetSocketAddress(8000)); Selector selector = Selector.open(); serverSocketChannel....
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); Selector selector = Selector.open(); ``` 2. **绑定端口并设置非阻塞模式**: ```java serverSocketChannel.socket().bind(new ...
1. **创建ServerSocketChannel并绑定端口**:通过`ServerSocketChannel.open()`创建一个服务器端通道,并使用`bind()`方法将它绑定到指定的端口,等待客户端连接。 2. **切换到非阻塞模式**:为了让服务端能处理多...
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().bind(new InetSocketAddress(12345)); // 创建...
ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel client = server.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); } ...
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.bind(new InetSocketAddress(8080)); serverSocketChannel.configureBlocking(false); serverSocketChannel.register...
2. 创建或获取ServerSocketChannel:`ServerSocketChannel serverChannel = ServerSocketChannel.open();` 3. 配置为非阻塞模式:`serverChannel.configureBlocking(false);` 4. 注册Selector并指定关注的事件:`...
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 设置为非阻塞模式 serverSocketChannel.configureBlocking(false); // 绑定端口 serverSocketChannel.socket().bind(new ...
常见的通道类型有FileChannel、SocketChannel、ServerSocketChannel等。 2. **缓冲区(Buffer)**:缓冲区是数据操作的主要对象,所有对数据的操作都先发生在缓冲区上。缓冲区提供了一种有限容量的数据容器,可以通过...
1. **绑定ServerSocketChannel**:服务器启动时,会在指定的端口上创建并绑定一个ServerSocketChannel,这使得服务器可以监听特定端口上的连接请求。 2. **配置选择器(Selector)**:NioEventLoop使用Java NIO的...
5. ServerSocketChannel类:ServerSocketChannel是一个用于监听来自客户端的连接请求的通道,可以看作是非阻塞模式的ServerSocket。 6. SocketChannel类:SocketChannel是一个连接通道,可以看作是非阻塞模式的...