⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectasync.java

📁 java nio 编程一个实例子.服务端程序
💻 JAVA
字号:
package com.ronsoft.books.nio.channels;import java.nio.channels.SocketChannel;import java.net.InetSocketAddress;/** * Demonstrate asynchronous connection of a SocketChannel. * * Created: April 2002 * @author Ron Hitchens (ron@ronsoft.com) * @version $Id: ConnectAsync.java,v 1.2 2002/04/28 01:47:58 ron Exp $ */public class ConnectAsync{	public static void main (String [] argv)		throws Exception	{		String host = "localhost";		int port = 80;		if (argv.length == 2) {			host = argv [0];			port = Integer.parseInt (argv [1]);		}		InetSocketAddress addr = new InetSocketAddress (host, port);		SocketChannel sc = SocketChannel.open();		sc.configureBlocking (false);		System.out.println ("initiating connection");		sc.connect (addr);		while ( ! sc.finishConnect()) {			doSomethingUseful();		}		System.out.println ("connection established");		// Do something with the connected socket		// The SocketChannel is still non-blocking		sc.close();	}	private static void doSomethingUseful()	{		System.out.println ("doing something useless");	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -