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

📄 serversocketlistener.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.tcp;import java.io.IOException;import java.io.InterruptedIOException;import java.net.InetSocketAddress;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import org.butor.log.Log;/** * This object handle server socket listening * and welcoming. This object is runnable. it run in a thread * and listen for incoming client sockets and then relay * theses clients to another object for handling. that object implements * the interface IClientSocketHandler that this class use to comunicate with it. *  * @author Aiman Sawan */public class ServerSocketListener implements Runnable {	// handle the port number to listen to.	protected int f_tcpPort;	protected ServerSocketChannel f_serverSocket;	// object that will handle incoming client sockets	protected ISocketConnectionHandler f_socketConnectionHandler;	protected Thread f_serverSocketThread = null;	protected boolean f_shutdown;	/**	 * Constructor.	 * 	 * @param tcpPort int, the port number to listen to.	 * @param handler ISocketConnectionHandler, object that will 	 * 		handle incoming socket connections	 */	public ServerSocketListener(int tcpPort, ISocketConnectionHandler handler)		throws IOException {		if (handler == null) {			throw new IOException("Got null ISocketConnectionHandler!");		}		f_tcpPort = tcpPort;		f_socketConnectionHandler = handler;		f_shutdown = false;		// Create a server socket to accept connections		f_serverSocket = ServerSocketChannel.open();		f_serverSocket.socket().bind(new InetSocketAddress(f_tcpPort));		start();	}	/**	 * get shutdown status.	 * 	 * @return boolean, shutdwon flag.	 */	public boolean isShutdown() {		return f_shutdown;	}	/**	 * run the thread	 */	public void run() {		Log.logStr(this, Log.LOG_TYPE_INFO, "run()", "Start...");		while (!f_shutdown) {			// As connection comes in, give them to the org.butor.web.listener.			try {				if (f_socketConnectionHandler == null) {					Log.logStr(						Log.LOG_LEVEL_LOW,						this,						Log.LOG_TYPE_INFO,						"run()",						"Got null ISocketConnectionHandler! Shutting down.");					shutdown();				}				// set receive timeout to be able to shutdown.				// if the jvm does not close the socket when the close() are invoked.				Log.logStr(this, Log.LOG_TYPE_INFO, "run()", "Listening for client connections ...");								SocketChannel socket = f_serverSocket.accept();				if (socket != null) {					Log.logStr(this, Log.LOG_TYPE_INFO, "run()", "Got client connection. " +socket);					SocketConnection conn = new SocketConnection();					conn.setSocket(socket);					Log.logStr(this, Log.LOG_TYPE_INFO, "run()", "Passing socket to handler. Handler=[" +f_socketConnectionHandler.getClass().getName() +"]");										f_socketConnectionHandler.handleSocketConnection(conn);				} else {					Log.logStr(this, Log.LOG_TYPE_INFO, "run()", "Got NULL client connections!");				}			} catch (InterruptedIOException e) {				// OK for time out				if (f_shutdown) {					break;				}							} catch (IOException e) {				Log.logStr(					Log.LOG_LEVEL_LOW,					this,					Log.LOG_TYPE_INFO,					"run()",					e.getMessage());			}		}		Log.logStr(this, Log.LOG_TYPE_INFO, "run()", "Stopped.");	}	/**	 * Shutdown this object	 */	public void shutdown() {		f_shutdown = true;		if (f_serverSocket != null) {			// When the thread is killed, close the server socket			try {				f_serverSocket.close();							} catch (IOException e) {				Log.logException(this, Log.LOG_TYPE_ERROR, "shutdown()", e);			}		}	}	/**	 * run this thread.	 * keep a reference on this thread to interrupt it when	 * shutting down.	 */	protected void start() {		if (f_shutdown) {			return;		}		Log.logStr(this, Log.LOG_TYPE_INFO, "start()", "will listen for tcp connections on port [" +f_tcpPort +"].");		f_serverSocketThread =			new Thread(this, "Server_Socket_Listener_on_port_" + f_tcpPort);		f_serverSocketThread.start();	}}

⌨️ 快捷键说明

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