创建非

欢马劈雪     最近更新时间:2020-01-02 10:19:05

337
创建一个非阻塞套接字


import java.io.IOException;/*n o w  j a v a  . c o m 提供*/

import java.net.InetSocketAddress;

import java.nio.channels.SocketChannel;


public class Main {

  public static void main(String[] args) {

    try {

      // Create a non-blocking socket channel on port 80

      SocketChannel sChannel = createSocketChannel("hostname.com", 80);


      while (!sChannel.finishConnect()) {

        // Do something else

      }//n o w j a v a . c o m - 时代Java

      // Socket channel is now ready to use

    } catch (IOException e) {

    }


  }


  public static SocketChannel createSocketChannel(String hostName, int port)

      throws IOException {

    // Create a non-blocking socket channel

    SocketChannel sChannel = SocketChannel.open();

    sChannel.configureBlocking(false);


 
展开阅读全文