Java Socket编程实例(四)- NIO TCP实践
作者:kingxss 发布时间:2021-10-07 13:44:35
标签:Java,Socket,NIO,TCP
一、回传协议接口和TCP方式实现:
1.接口:
import java.nio.channels.SelectionKey;
import java.io.IOException;
public interface EchoProtocol {
void handleAccept(SelectionKey key) throws IOException;
void handleRead(SelectionKey key) throws IOException;
void handleWrite(SelectionKey key) throws IOException;
}
2.实现:
import java.nio.channels.*;
import java.nio.ByteBuffer;
import java.io.IOException;
public class TCPEchoSelectorProtocol implements EchoProtocol{
private int bufSize; // Size of I/O buffer
public EchoSelectorProtocol(int bufSize) {
this.bufSize = bufSize;
}
public void handleAccept(SelectionKey key) throws IOException {
SocketChannel clntChan = ((ServerSocketChannel) key.channel()).accept();
clntChan.configureBlocking(false); // Must be nonblocking to register
// Register the selector with new channel for read and attach byte buffer
clntChan.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocate(bufSize));
}
public void handleRead(SelectionKey key) throws IOException {
// Client socket channel has pending data
SocketChannel clntChan = (SocketChannel) key.channel();
ByteBuffer buf = (ByteBuffer) key.attachment();
long bytesRead = clntChan.read(buf);
if (bytesRead == -1) { // Did the other end close?
clntChan.close();
} else if (bytesRead > 0) {
// Indicate via key that reading/writing are both of interest now.
key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
}
public void handleWrite(SelectionKey key) throws IOException {
/*
* Channel is available for writing, and key is valid (i.e., client channel
* not closed).
*/
// Retrieve data read earlier
ByteBuffer buf = (ByteBuffer) key.attachment();
buf.flip(); // Prepare buffer for writing
SocketChannel clntChan = (SocketChannel) key.channel();
clntChan.write(buf);
if (!buf.hasRemaining()) { // Buffer completely written?
//Nothing left, so no longer interested in writes
key.interestOps(SelectionKey.OP_READ);
}
buf.compact(); // Make room for more data to be read in
}
}
二、NIO TCP客户端:
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class TCPEchoClientNonblocking {
public static void main(String args[]) throws Exception {
String server = "127.0.0.1"; // Server name or IP address
// Convert input String to bytes using the default charset
byte[] argument = "0123456789abcdefghijklmnopqrstuvwxyz".getBytes();
int servPort = 5500;
// Create channel and set to nonblocking
SocketChannel clntChan = SocketChannel.open();
clntChan.configureBlocking(false);
// Initiate connection to server and repeatedly poll until complete
if (!clntChan.connect(new InetSocketAddress(server, servPort))) {
while (!clntChan.finishConnect()) {
System.out.print("."); // Do something else
}
}
ByteBuffer writeBuf = ByteBuffer.wrap(argument);
ByteBuffer readBuf = ByteBuffer.allocate(argument.length);
int totalBytesRcvd = 0; // Total bytes received so far
int bytesRcvd; // Bytes received in last read
while (totalBytesRcvd < argument.length) {
if (writeBuf.hasRemaining()) {
clntChan.write(writeBuf);
}
if ((bytesRcvd = clntChan.read(readBuf)) == -1) {
throw new SocketException("Connection closed prematurely");
}
totalBytesRcvd += bytesRcvd;
System.out.print("."); // Do something else
}
System.out.println("Received: " + // convert to String per default charset
new String(readBuf.array(), 0, totalBytesRcvd).length());
clntChan.close();
}
}
三、NIO TCP服务端:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.*;
import java.util.Iterator;
public class TCPServerSelector {
private static final int BUFSIZE = 256; // Buffer size (bytes)
private static final int TIMEOUT = 3000; // Wait timeout (milliseconds)
public static void main(String[] args) throws IOException {
int[] ports = {5500};
// Create a selector to multiplex listening sockets and connections
Selector selector = Selector.open();
// Create listening socket channel for each port and register selector
for (int port : ports) {
ServerSocketChannel listnChannel = ServerSocketChannel.open();
listnChannel.socket().bind(new InetSocketAddress(port));
listnChannel.configureBlocking(false); // must be nonblocking to register
// Register selector with channel. The returned key is ignored
listnChannel.register(selector, SelectionKey.OP_ACCEPT);
}
// Create a handler that will implement the protocol
TCPProtocol protocol = new TCPEchoSelectorProtocol(BUFSIZE);
while (true) { // Run forever, processing available I/O operations
// Wait for some channel to be ready (or timeout)
if (selector.select(TIMEOUT) == 0) { // returns # of ready chans
System.out.print(".");
continue;
}
// Get iterator on set of keys with I/O to process
Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
while (keyIter.hasNext()) {
SelectionKey key = keyIter.next(); // Key is bit mask
// Server socket channel has pending connection requests?
if (key.isAcceptable()) {
System.out.println("----accept-----");
protocol.handleAccept(key);
}
// Client socket channel has pending data?
if (key.isReadable()) {
System.out.println("----read-----");
protocol.handleRead(key);
}
// Client socket channel is available for writing and
// key is valid (i.e., channel not closed)?
if (key.isValid() && key.isWritable()) {
System.out.println("----write-----");
protocol.handleWrite(key);
}
keyIter.remove(); // remove from set of selected keys
}
}
}
}


猜你喜欢
- 前言在开发中常要处理横竖屏切换,怎么处理先看生命周期申明Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是A
- 一、什么是方法的重载?方法的重载是指一个类中可以定义多个方法名相同,但参数不同的方法。调用时,会根据不同的参数自动匹配对应的方法。二、构成方
- 描述输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数输入描述:控制台随机输入一串字符串输出描述:输出字符串中包含的英文字母
- 1. 新建项目引入web和security包完整的pom.xml文件如下<?xml version="1.0" e
- 前言前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务my
- 一般情况下,我们大部分人都是在XML文件直接写布局,可是有些时候需要用代码动态添加布局,比如我昨天做一个viewpager的页数
- 目录算术操作符移位操作符位操作符赋值操作符 单目操作符(类型) 强制类型转换 &n
- 本文实例为大家分享了Android实现选项菜单的具体代码,供大家参考,具体内容如下创建选项菜单步骤:(1)覆盖Activity的onCrea
- 在C语言中,想要获取字符串长度可以有很多方法,下面分别介绍一、使用sizeof()运算符在C语言中,sizeof() 是长度的运算符,括号中
- 1,现在因为遇到一个读取pdf文件文本信息遇到乱么问题,才找到这个文本字符串的编码转换的实现方式来判断是否存在乱码(0>乱码>2
- 一、使用Optional引言1.1、代码问题引出在写程序的时候一般都遇到过 NullPointerException,所以经常会对程序进行非
- 目录IntroSampleWhat insideMoreReferenceIntroC# 9 中引入了 record,record 是一个特
- 这篇文章主要介绍了简单了解java标识符的作用和命名规则,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 本文实例中的自定义类PictureBox继承于UserControl,最终实现简单的分屏功能。分享给大家供大家参考之用。具体实现代码如下:p
- 本文通过优化买票的重复流程来说明享元模式,为了加深对该模式的理解,会以String和基本数据类型的包装类对该模式的设计进一步说明。读者可以拉
- 悲观锁、乐观锁简介: 悲观锁:同步操作。即用户A在操作某条数据时,为其上锁,限制其他用户操作,用户A操作完成提交事务后其他用户方可
- 一、JTA组件简介什么是JTAJTA,全称:Java Transaction API。JTA事务比JDBC事务更强大。一个JTA事务可以有多
- 先给大家展示下效果图,如果大家感觉还不错,请参考实现代码很简单的一个例子,点击刷新验证码,刷新当前显示的验证码,点击确定,如果输入的和显示的
- 前言有时候我们在项目中,会用到一些本地 jar 包文件,比如隔壁公司自己打包的;此时无法从maven远程仓库拉取;那么我们可以考虑把 jar
- 测试参数设置:1、循环调用new A()实现堆溢出,java.lang.OutOfMemoryError: Java heap space,