Java Socket实现基于NIO的TCP通信

一、NIO的引入

对于BIO通信时,服务端ServerSocket会在accept方法,和read方法发生堵塞,导致别的请求无法进入,当然可以使用多线程处理,如果请求很多,就会有很多线程,消耗太大。NIO服务端ServerSocketChannel设置为非阻塞,把通道注册到选择器Selector上,多个客户端请求,都注册到选择器Selector上,由选择器轮询已经准备好的通道进行处理。

二、Java NIO的核心

Buffer、Channel、Selector。

Buffer缓冲区可进行读写,put写数据,flip方法转换为读数据,clear清空缓冲区。

Channel有ServerSocketChannel、SocketChannel,通道可进行读写操作。

Seletor会监听connect、accept、read、write事件。

三、细说Selector

  • Selector注册:

通道调用register方法,把通道注册到Selector上,第一个参数是Selector,第二个参数是interest集合,意思是Selector监听channel时,对什么事件感兴趣,包含connect、accept、read、write事件。

ssChannel.register(selector, SelectionKey.OP_ACCEPT);
  • SelectionKey:

当想Seletor注册channel是,会返回SelectionKey对象,这个对象也会保存在选择器Selector中,SelectionKey包含interest、channel属性,也就算说可以通过SelectionKey获得interest、channel。

  • selector.keys()与selector.selectKeys():

channel注册到selector,会把SelectionKey放到SelectorImp对象的Set<SelectionKey> publicKeys,通过seletor.keys可获得;当通道发出对应的interest事件,会放到Set<SelectionKey> publicSelectedKeys中,通过seletor.seletKeys可获得。例如,ServerSocketChannel的accept事件注册到Selector,当没有请求时,不会放到publicSelectedKeys中,有请求时,会把ServerSocketChannel的SelectionKey放到publicSelectedKeys。客户端,发出数据时,才把客户端通道放入publicSelectedKeys。

  • seletor.selector()

selector.selector方法返回的是publicSelectedKeys集合的大小,也可以说,触发事件通道的个数。如果返回的数量为0,则会处于阻塞状态。

四、服务端代码

public class NIOServer {

    public static void main(String[] args) {
        try {
            openServer(8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void openServer(int port) throws IOException {
//        打开通道
        ServerSocketChannel ssChannel = ServerSocketChannel.open();
//        设置为非阻塞
        ssChannel.configureBlocking(false);
//        绑定端口
        ssChannel.socket().bind(new InetSocketAddress(port));
//        选择器
        Selector selector = Selector.open();
//        把通道接受事件注册到选择器
        ssChannel.register(selector, SelectionKey.OP_ACCEPT);
//        当被选中的通道个数大于0时,进行业务处理
        while (selector.select() > 0){
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();
            while (it.hasNext()){
                SelectionKey sk = it.next();
                if(sk.isAcceptable()){
//                    得到发出请求连接的通道
                    ServerSocketChannel channel = (ServerSocketChannel) sk.channel();
                    SocketChannel sChannel = channel.accept();
//                    通道设置非阻塞
                    sChannel.configureBlocking(false);
//                    把通道读事件注册到选择器
                    sChannel.register(selector, SelectionKey.OP_READ);
                }
                else if(sk.isReadable()){
//                    得到发出读请求的通道
                    SocketChannel sChannel = (SocketChannel) sk.channel();
//                      定义一个缓冲区
                    ByteBuffer buffer = ByteBuffer.allocate(1024);

                    int len = 0;
//                    len为0时,无数据可读;len为-1时,客户端关闭了socket
                    while ((len = sChannel.read(buffer)) > 0){
//                        更改缓冲区为读模式
                        buffer.flip();
                        String str = new String(buffer.array(), 0, len);
                        System.out.println(str);
//                        清空缓冲区
                        buffer.clear();
                    }
//                    写响应数据
                    buffer.put("server response".getBytes());
                    buffer.flip();
                    sChannel.write(buffer);
                    buffer.clear();

                    if (len == -1){
                        sChannel.close();
                    }
                }
//               取消
                it.remove();
            }
        }
    }

}

五、客户端代码

public class NIOClient {
    public static void main(String[] args) {
        try {
            connect("127.0.0.1", 8888);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void connect(String host, int port) throws IOException {
//        打开通道
        SocketChannel sChannel = SocketChannel.open();
//        通道设置为非阻塞
        sChannel.configureBlocking(false);
//        连接
        sChannel.connect(new InetSocketAddress(host, port));
//        选择器
        Selector selector = Selector.open();
//        把通道注册到选择器,监听读事件
        sChannel.register(selector, SelectionKey.OP_READ);

        while (!sChannel.finishConnect());

//        开启一个线程读数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                //            定义缓冲区
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                try {
//                    轮询
                    while (selector.select() > 0){
                        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
                        while (it.hasNext()){
                            SelectionKey sk = it.next();
//                            读事件
                            if (sk.isReadable()){
                                SocketChannel channel = (SocketChannel) sk.channel();
                                int len = 0;
                                while ((len = sChannel.read(buffer))> 0){
                                    sChannel.read(buffer);
                                    buffer.flip();
                                    String str = new String(buffer.array(), 0, len);
                                    System.out.println(str);
                                    buffer.clear();
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

//        主线程写数据
        Scanner sc = new Scanner(System.in);
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true){
            String str =sc.next();
            if("quit".equals(str)){
                break;
            }
            buffer.put(str.getBytes());
            buffer.flip();
            sChannel.write(buffer);
            buffer.clear();
        }
//        关闭通道
        sChannel.close();
    }
}

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ModbusTCP是一种基于TCP/IP协议的通信协议,可以实现不同设备之间的数据交换。下面是Java实现ModbusTCP通信的基本步骤: 1. 创建Socket连接:使用JavaSocket类创建TCP连接。 ```java Socket socket = new Socket(ipAddress, port); ``` 2. 获取输入输出流:使用Socket类的getInputStream()和getOutputStream()方法获取输入输出流。 ```java InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); ``` 3. 发送请求数据:以ModbusTCP协议格式发送请求数据。 4. 接收响应数据:从输入流中读取响应数据,并解析成ModbusTCP协议格式。 5. 解析响应数据:解析ModbusTCP协议格式的响应数据,并提取需要的数据。 下面是一个简单的Java示例代码实现ModbusTCP通信: ```java import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.ByteBuffer; import java.util.Arrays; public class ModbusTcpClient { public static void main(String[] args) throws IOException { String ipAddress = "192.168.0.1"; int port = 502; // 创建Socket连接 Socket socket = new Socket(ipAddress, port); // 获取输入输出流 InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); // 发送请求数据 byte[] request = new byte[] {0, 0, 0, 0, 0, 6, 1, 3, 0, 0, 0, 2}; outputStream.write(request); // 接收响应数据 byte[] response = new byte[1024]; int readBytes = inputStream.read(response); // 解析响应数据 byte[] data = Arrays.copyOfRange(response, 9, readBytes); ByteBuffer buffer = ByteBuffer.wrap(data); int value1 = buffer.getShort(); int value2 = buffer.getShort(); System.out.println("Value 1: " + value1); System.out.println("Value 2: " + value2); // 关闭连接 socket.close(); } } ``` 在这个示例中,我们向ModbusTCP服务器发送了一个读取连续寄存器的请求,并且获取了两个16位寄存器的值。实际使用时,需要根据具体的需求和设备协议进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值