Java--NIO-TCP Socket

Posted 11楼的日记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java--NIO-TCP Socket相关的知识,希望对你有一定的参考价值。

1、首先我们使用SocketChannel,实现socket客户端

package com.seeyon.nio.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Created by yangyu on 2017/2/22.
 */
public class Client {

    public static void main(String[] args) {

        try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 8088))) {
            socketChannel.configureBlocking(false);
            ByteBuffer byteBuffer = ByteBuffer.allocate(512);
            socketChannel.write(ByteBuffer.wrap("this is client send message".getBytes()));

            while (true) {
                byteBuffer.clear();
                int readBytes = socketChannel.read(byteBuffer);
                if (readBytes > 0) {
                    byteBuffer.flip();
                    System.out.println(new String(byteBuffer.array(), 0, readBytes));
                    socketChannel.close();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

 

2、使用ServerSocketChannel实现服务端,并且使用Selector

package com.seeyon.nio.socket;

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;
import java.util.Set;

/**
 * Created by yangyu on 2017/2/22.
 */
public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        serverChannel.socket().bind(new InetSocketAddress(8088));
        serverChannel.configureBlocking(false);
        Selector selector = Selector.open();
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();

            Set<SelectionKey> readyKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = readyKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();
                if (key.isAcceptable()) {
                    System.out.println("accept");
                    ByteBuffer byteBuffer = ByteBuffer.allocate(512);
                    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    int readKeys = socketChannel.read(byteBuffer);
                    if (readKeys > 0) {
                        byteBuffer.flip();
                        System.out.println(new String(byteBuffer.array(), 0, readKeys));
                    }
                    socketChannel.write(ByteBuffer.wrap("接收到了".getBytes()));

                } else if (key.isReadable()) {
                    System.out.println("read");

                } else if (key.isWritable()) {
                    System.out.println("write");

                }
            }
        }
    }
}

 

以上是关于Java--NIO-TCP Socket的主要内容,如果未能解决你的问题,请参考以下文章

Python 之 Socket编程(TCP/UDP)

Python干货socket中的listen()参数(数字)到底代表什么?

paper 78:sniff抓包程序片段

通过 c# 与 socket.io 服务器通信

微信小程序代码片段

VSCode自定义代码片段——CSS选择器