Create a SocketChannel.

@return
the SocketChannel.
@throws IOException
the IOException.
334364
SocketChannel createSock() throws IOException {
        SocketChannel sock;
        sock = SocketChannel.open();
        sock.configureBlocking(false);
        sock.socket().setSoLinger(false, -1);
        sock.socket().setTcpNoDelay(true);
        return sock;
    }
787523
public T open(RequestChannel channel)
    {
        return clientManager.createClient(channel, clientType, clientName, eventHandlers);
    }
1366937
protected final void configureSocketChannel(SocketChannel sc) throws IOException {
    sc.socket().setSoTimeout(this.soTimeout);
    sc.configureBlocking(false);
    if (this.socketOptions.get(StandardSocketOption.SO_REUSEADDR) != null) {
      sc.socket().setReuseAddress(StandardSocketOption.SO_REUSEADDR.type()
          .cast(this.socketOptions.get(StandardSocketOption.SO_REUSEADDR)));
    }
    if (this.socketOptions.get(StandardSocketOption.SO_SNDBUF) != null) {
      sc.socket().setSendBufferSize(StandardSocketOption.SO_SNDBUF.type()
          .cast(this.socketOptions.get(StandardSocketOption.SO_SNDBUF)));
    }
    if (this.socketOptions.get(StandardSocketOption.SO_KEEPALIVE) != null) {
      sc.socket().setKeepAlive(StandardSocketOption.SO_KEEPALIVE.type()
          .cast(this.socketOptions.get(StandardSocketOption.SO_KEEPALIVE)));
    }
    if (this.socketOptions.get(StandardSocketOption.SO_LINGER) != null) {
      sc.socket().setSoLinger(this.soLingerOn, StandardSocketOption.SO_LINGER.type()
          .cast(this.socketOptions.get(StandardSocketOption.SO_LINGER)));
    }
    if (this.socketOptions.get(StandardSocketOption.SO_RCVBUF) != null) {
      sc.socket().setReceiveBufferSize(StandardSocketOption.SO_RCVBUF.type()
          .cast(this.socketOptions.get(StandardSocketOption.SO_RCVBUF)));

    }
    if (this.socketOptions.get(StandardSocketOption.TCP_NODELAY) != null) {
      sc.socket().setTcpNoDelay(StandardSocketOption.TCP_NODELAY.type()
          .cast(this.socketOptions.get(StandardSocketOption.TCP_NODELAY)));
    }
  }
309512
private static AsynchronousSocketChannel createChannel(InetSocketAddress address, int poolSize) {

		AsynchronousSocketChannel channel;
		try {
			AsynchronousChannelGroup group = AsynchronousChannelGroup.withFixedThreadPool(//
					poolSize, // 默认线程池大小
					ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()//
			);
			channel = AsynchronousSocketChannel.open(group);
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}

		try {
			channel.connect(address).get();
		} catch (InterruptedException | ExecutionException e) {
			throw new SocketRuntimeException(e);
		}
		return channel;
	}
1342970
static ServerSocketChannelUDT newAcceptorChannelUDT(
            final TypeUDT type) {
        try {
            return SelectorProviderUDT.from(type).openServerSocketChannel();
        } catch (final IOException e) {
            throw new ChannelException("failed to open a server socket channel", e);
        }
    }
939976
public Socket createSocket(SocketType type)
    {
        // Create and register socket
        Socket socket = context.socket(type);
        socket.setRcvHWM(this.rcvhwm);
        socket.setSndHWM(this.sndhwm);
        sockets.add(socket);
        return socket;
    }
841458
protected Channel createChannel(ChannelData channelData) throws ChannelException {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
            Tr.entry(tc, "createChannel");
        }

        TCPChannelConfiguration newCC = new TCPChannelConfiguration(channelData);
        TCPChannel channel = null;

        boolean isOverrideClassUsed = false;
        if ((!newCC.isNIOOnly())
            && (commClass != null)
            && (ChannelFrameworkImpl.getRef().getAsyncIOEnabled())) {
            try {
                channel = (TCPChannel) commClass.newInstance();
                ChannelTermination ct = channel.setup(channelData, newCC, this);
                if (ct != null) {
                    this.terminationList.put(commClassName, ct);
                }
                isOverrideClassUsed = true;
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "using CommClass: " + commClass);
                }
            } catch (Exception e) {
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "Exception trying to instantiate CommClass: " + e);
                }
            }
        }

        if (!isOverrideClassUsed) {
            channel = new NioTCPChannel();
            ChannelTermination ct = channel.setup(channelData, newCC, this);
            if (ct != null) {
                this.terminationList.put("NioTCPChannel", ct);
            }
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) {
            Tr.exit(tc, "createChannel");
        }
        return channel;
    }
1194287
public void writeTo(SocketChannel channel) throws IOException {
        channel.write(buffer);

        // only if buffer is empty
        if (buffer.remaining() == 0) {
            buffer.clear();
            state = BufferState.READY_TO_WRITE;
        }
    }
1067362
public static ProductChannel createChannel(@Nullable ShoppingProductChannel channel) {
    ProductChannel productChannel = new ProductChannel();
    productChannel.setChannel(channel);
    return productChannel;
  }
208967
public static ServerSocketBar create(int port, int listenBacklog)
    throws IOException
  {
    return create(null, port, listenBacklog, true);
  }
124451
protected synchronized void create(boolean stream) throws IOException {
        this.stream = stream;

        if (!stream) {
            ResourceManager.beforeUdpCreate();
            try {
                socketCreate(false);
            } catch (IOException ioe) {
                ResourceManager.afterUdpClose();
                throw ioe;
            }
        } else {
            socketCreate(true);
        }
        if (socket != null)
            socket.setCreated();
        if (serverSocket != null)
            serverSocket.setCreated();

        // socketCreate will set |fd| if it succeeds.
        if (fd != null && fd.valid()) {
            guard.open("close");
        }
    }
1045979
static ClientRequestResponseChannel clientChannel(
          final Configuration configuration,
          final ResponseChannelConsumer consumer,
          final Logger logger) throws Exception {
    return new ClientRequestResponseChannel(
            configuration.addressOfHost,
            consumer,
            configuration.readBufferPoolSize,
            configuration.readBufferSize,
            logger);
  }
339801
public SSLSocket newSslSocket() throws IOException {
        SSLSocketFactory factory = sslContext.getSocketFactory();
        
        SSLSocket socket = (SSLSocket)factory.createSocket();
        
        if (sslConfig.getWantClientAuth())
            socket.setWantClientAuth(sslConfig.getWantClientAuth());
        if (sslConfig.getNeedClientAuth())
            socket.setNeedClientAuth(sslConfig.getNeedClientAuth());

        socket.setEnabledCipherSuites(selectCipherSuites(socket.getEnabledCipherSuites(),
							 socket.getSupportedCipherSuites()));   
        socket.setEnabledProtocols(selectProtocols(socket.getEnabledProtocols(),socket.getSupportedProtocols()));

        return socket;
    }
553804
private synchronized Channel channel() throws IOException {
        if (autoReconnect) {
            try {
                connect();
            } catch (GeneralSecurityException e) {
                throw new IOException(e);
            }
        }

        return channel;
    }
1273305
private static Channel createChannel() throws IOException {

        // dup the file descriptor - we do this so that for two reasons :-
        // 1. Avoids any timing issues with FileDescriptor.in being closed
        //    or redirected while we create the channel.
        // 2. Allows streams based on file descriptor 0 to co-exist with
        //    the channel (closing one doesn't impact the other)

        int fdVal = dup(0);

        // Examine the file descriptor - if it's not a socket then we don't
        // create a channel so we release the file descriptor.

        int st;
        st = soType0(fdVal);
        if (st != SOCK_STREAM && st != SOCK_DGRAM) {
            close0(fdVal);
            return null;
        }


        // Next we create a FileDescriptor for the dup'ed file descriptor
        // Have to use reflection and also make assumption on how FD
        // is implemented.

        Class paramTypes[] = { int.class };
        Constructor ctr = Reflect.lookupConstructor("java.io.FileDescriptor",
                                                    paramTypes);
        Object args[] = { new Integer(fdVal) };
        FileDescriptor fd = (FileDescriptor)Reflect.invoke(ctr, args);


        // Now create the channel. If the socket is a streams socket then
        // we see if tthere is a peer (ie: connected). If so, then we
        // create a SocketChannel, otherwise a ServerSocketChannel.
        // If the socket is a datagram socket then create a DatagramChannel

        SelectorProvider provider = SelectorProvider.provider();
        assert provider instanceof sun.nio.ch.SelectorProviderImpl;

        Channel c;
        if (st == SOCK_STREAM) {
            InetAddress ia = peerAddress0(fdVal);
            if (ia == null) {
               c = new InheritedServerSocketChannelImpl(provider, fd);
            } else {
               int port = peerPort0(fdVal);
               assert port > 0;
               InetSocketAddress isa = new InetSocketAddress(ia, port);
               c = new InheritedSocketChannelImpl(provider, fd, isa);
            }
        } else {
            c = new InheritedDatagramChannelImpl(provider, fd);
        }
        return c;
    }
285175
protected Channel getConsumerChannel() throws IOException, TimeoutException {
        if (consumerChannel == null) {
            synchronized (this) {
                if (consumerChannel == null) {
                    consumerChannel = createChannel();
                }
            }
        }
        return consumerChannel;
    }
913717
public static IOException create(String msg, IOException exn)
  {
    if (exn.getClass().equals(IOException.class)
        || exn.getClass().equals(java.net.SocketException.class)
        || exn.getClass().getName().equals("javax.net.ssl.SSLException")) {
      return new ClientDisconnectException(msg, exn);
    }
    else
      return exn;
  }