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

📄 connserver.java

📁 一个实用工具类
💻 JAVA
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file.  */package org.butor.socket.nio;import java.io.IOException;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.nio.channels.ServerSocketChannel;import org.butor.log.Log;/** * Server Socket manager. This class listen on a given port and notify a regitered * handler for incoming connections. *  * @author sawanai * Mar 25, 2004 */public class ConnServer {	protected int f_servicePort = -1;	protected String f_serviceHost = null; 	protected IConnManager f_connectionManager = null;	protected IConnHandler f_connHandler = null;	protected boolean f_shutdown = false;	protected ServerSocketChannel f_serverChannel = null;		/**	 * 	 */	public ConnServer(int servicePort, IConnManager connectionManager, IConnHandler connHandler) throws IOException {		this(servicePort, null, connectionManager, connHandler);	}	/**	 * 	 */	public ConnServer(int servicePort, String serviceHost, IConnManager connectionManager, IConnHandler connHandler) throws IOException {		super();				f_servicePort = servicePort;		f_serviceHost = serviceHost;		f_connectionManager = connectionManager;		f_connHandler = connHandler;				//servicing conection ----------------- 		//Server Socket Channel		f_serverChannel = ServerSocketChannel.open();		//Server Socket to bind with the channel		ServerSocket serverSocket = f_serverChannel.socket();				//Bind		serverSocket.setReuseAddress(true);		if (f_serviceHost == null) {			serverSocket.bind(new InetSocketAddress(f_servicePort));		} else {			serverSocket.bind(new InetSocketAddress(f_serviceHost, f_servicePort));		}		// server socket blocking mode ?		f_serverChannel.configureBlocking(false);				f_connectionManager.registerWithSelector(f_serverChannel, f_connHandler);		f_connectionManager.wantAccept(f_serverChannel);				Log.logStr(this, Log.INFO, "ConnServer", 			"Will listen on host [" +(f_serviceHost == null ? "any" : f_serviceHost) +"] + port [" +f_servicePort +"] for handler [" +f_connHandler.getHandlerName() +"] ...");	}		/**	 * shutdown.	 */	public void shutdown() {		f_shutdown = true;		Log.logStr(this, Log.INFO, "shutdown()",  "shutting down ...");		if (f_serverChannel != null) {			f_connectionManager.unregisterWithSelector(f_serverChannel);		}		try {			f_serverChannel.close();					} catch (IOException e) {			//OK					} finally {			Log.logStr(this, Log.INFO, "shutdown()",  "Done.");		}	}		public String getHostName() {		return f_serviceHost;	}}

⌨️ 快捷键说明

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