首页 > 代码库 > Java NIO SocketChannel客户端例子(支持连接失败后自动重连)

Java NIO SocketChannel客户端例子(支持连接失败后自动重连)

这两天想找找标题里说的这个示例代码,发现网上这么多教程,连怎么样实现自动重连都不讲,所以把自己写的例子贴上来。仅仅使用递归,不使用多线程,就可以实现初步的目的:

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Timer;


public class NIOClient {
	// 信道选择器
	private Selector selector;

	// 与服务器通信的信道
	SocketChannel socketChannel;

	// 要连接的服务器IP地址
	private String hostIp;

	// 要连接的远程服务器在监听的端口
	private int hostListenningPort;
	
	private static boolean timeTocken=false;
	private static Timer timer = new Timer();
	private static boolean timerSet=false;

	/**
	 * 构造函数
	 * 
	 * @param HostIp
	 * @param HostListenningPort
	 * @throws IOException
	 */
	public NIOClient(String HostIp, int HostListenningPort) throws IOException {
		this.hostIp = HostIp;
		this.hostListenningPort = HostListenningPort;
		initialize();
	}

	private void initialize() throws IOException {
		// 打开监听信道并设置为非阻塞模式
		try{
		socketChannel = SocketChannel.open(new InetSocketAddress(hostIp,
				hostListenningPort));
		}
		catch(ConnectException e){
			System.out.println("Error happened when establishing connection, try again 5s later");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			if(!timerSet){
				timer.schedule(new SetTocken(), 15000);
				timerSet=true;
			}
			if(!timeTocken){
				initialize();
			}
			return;
		}
		socketChannel.configureBlocking(false);
		// 打开并注册选择器到信道
		selector = Selector.open();
		socketChannel.register(selector, SelectionKey.OP_READ);
	}

	/**
	 * 发送字符串到服务器
	 * 
	 * @param message
	 * @throws IOException
	 */
	public void sendMsg(String message) throws IOException {
		ByteBuffer writeBuffer = ByteBuffer.wrap(message.getBytes("UTF-8"));
		socketChannel.write(writeBuffer);
	}

	public static void main(String[] args) throws IOException {
		NIOClient client = new NIOClient("127.0.0.1", 12000);
		client.sendMsg("This is a NIOClient, testing");
		client.end();
		timer.cancel();
	}
	
	private void end() {
		// TODO Auto-generated method stub
		try {
			socketChannel.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static class SetTocken extends java.util.TimerTask {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			timeTocken=true;
		}
	}
}


Java NIO SocketChannel客户端例子(支持连接失败后自动重连)