socketchannel为什么是抽象

2025-05-14 01:15:42
推荐回答(1个)
回答1:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;

public class TCPServer {
// 缓冲区大小
private static final int BufferSize = 1024;
// 超时时间,单位毫秒
private static final int TimeOut = 3000;
// 本地监听端口
private static final int ListenPort = 1978;
public static void main(String[] args) throws IOException {
// 创建选择器
Selector selector = Selector.open();
// 打开监听信道
ServerSocketChannel listenerChannel = ServerSocketChannel.open();
// 与本地端口绑定
listenerChannel.socket().bind(new InetSocketAddress(ListenPort));
// 设置为非阻塞模式