package mailclass;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
/**
*
* @author AAries zyz!
*/
public class Client extends Thread {
private Selector selector;
public Client(Selector selector) {
this.selector = selector;
Thread thread = new Thread(this);
thread.start();;
}
@Override
public void run() {
try {
while (selector.select() > 0) {
// 遍历每个有可用IO操作Channel对应的SelectionKey
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = (SelectionKey) it.next();
it.remove();
// 如果该SelectionKey对应的Channel中有可读的数据
if (sk.isReadable()) {
// 使用NIO读取Channel中的数据
SocketChannel socketChannel = (SocketChannel) sk.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
socketChannel.read(buffer);
buffer.flip();
// 将字节转化为为UTF-16的字符串
String receivedString = Charset.forName("UTF-16").newDecoder().decode(buffer).toString();
// 控制台打印出来
System.out.println("接收到来自服务器" + socketChannel.socket().getRemoteSocketAddress() + "的信息:" + receivedString);
// 为下一次读取作准备
// sk.interestOps(SelectionKey.OP_READ);//将键设为可读
}
if (sk.isWritable()) {
SocketChannel socketChannel = (SocketChannel) sk.channel();
ByteBuffer writeBuffer = ByteBuffer.wrap("我的程序员之道".getBytes("UTF-16"));
socketChannel.write(writeBuffer);
}
// 删除正在处理的SelectionKey
selector.selectedKeys().remove(sk);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
调用类的写法
/*
* 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.SocketChannel;
/**
*
* @author AAries zyz!
*/
class ClientTest {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open(new InetSocketAddress("172.16.22.11", 5200));
//打开一耳光SocketChannel并连接
sc.configureBlocking(false);
Selector selector = Selector.open();
sc.register(selector, SelectionKey.OP_READ);
//注册一个selector此时为空
Client client = new Client(selector);
//打开一个线程,如遇到可读的包则读取,如遇到可写的包,则写!
ByteBuffer writeBuffer=ByteBuffer.wrap("我的程序员之道".getBytes("UTF-16"));
sc.write(writeBuffer);
}
}
分享到:
相关推荐
SocketChannel非阻塞网络编程
java的ServerSocketChannel与SocketChannel的使用
一个关于SocketChannel、ServerSocketChannel、Selector的综合案例
这是一个非阻塞通信学习的基础模板,让你轻松掌握非阻塞通信。里面还包含了可运行的jar包,可以抢先体验效果哦~_~
NIO SSL 与阻塞IO不同,JVM不提供扩展基本套接字通道类的标准SSLSocketChannel和SSLServerSocketChannel类。 相反,必须使用手动编排SSL交换。 该项目提供了和,可以像和一样使用。入门直接下载您可以直接下载并将其...
一个 Java SocketChannel 实现,它使用提供的 Proxy 实例通过提供的代理建立网络连接。 SocketChannel 是通过表面下的 Socket 实例实现的。 限制 此实现目前仅支持阻塞模式。 请注意,这是 SocketChannel 实例的默认...
使用socket channel 连接服务器,断线重连,发送保活等的封装类。 Java NIO中的...打开一个SocketChannel并连接到互联网上的某台服务器。 一个新连接到达ServerSocketChannel时,会创建一个SocketChannel。
利用Socket实现聊天室实时聊天功能,包含服务器端以及客户端,输入指定IP和端口号进行监控
没有使用其他jar包 主要功能: 1、广场群聊,即发送的消息所有在线的人都能看到 2、聊天室群聊,可以创建聊天室,进入聊天室的人可以在里面聊天,创建的人或者是第一个人为管理员,拥有踢人的权限;...
【IT十八掌徐培成】Java基础第27天-02.NIO-ServerSocketChannel-SocketChannel.zip
NIO(服务端和客户端代码) 博文链接:https://songjianyong.iteye.com/blog/1757406
(用于TCP网络编程,客户端和服务器端都能用) ServerSocketChannel (用于TCP网络编程,专用与服务器端) 常见的Buffer,用来缓冲读写数据。 ByteBuffer MappedByteBuffer DirectByteBuffer HeapByteBuffer ...
实现了客户端和服务器之间的通信,服务器是用socketChannel实现,
● SocketChannel:Socket的替代类,支持阻塞通信与非阻塞通信。 ● Selector:为ServerSocketChannel监控接收连接就绪事件,为SocketChannel监控连接就绪、读就绪和写就绪事件。 ● SelectionKey:代表...
用JAVA写的一个FPT小程序,可实现简单的Ftp功能,Server端有Ui界面。
每次服务器端如果要向某个客户端发送消息,只需根据ClientId取出对应的SocketChannel,往里面写入message即可。心跳检测通过IdleEvent事件,定时向服务端放送Ping消息,检测SocketChannel是否终断。添加了...
SocketChannel被Selector检查,检查其声称可以接受的状态是否已经产生,如当SocketChannel在向Selector注册是设置了可接受状态为Read,此时当SocketChannel接收到数据后将进入可读状态。 如果需要实现一个线程或...
服务器端SNI :根据传入连接发送的接收到的,支持选择不同的 (SSLEngine完全不支持此功能,但Web浏览器和服务器普遍使用此功能)。 使用相同的API支持阻塞和非阻塞模式,就像SocketChannel对未加密的连接一样。 ...
此为手机移动端的代码,使用selector模型管理socketChannel,去连接服务器,实现了读写功能以及重连机制
2、客户端发送短信:通过SocketChannel向服务器发送数据。 服务器: 1、服务器初始化:定义EventLoopGroup,通过ServerBootstrap对服务器初始化,选择对外暴露的端口,初始化channel并绑定handler。 3、用户记录: ...