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

📄 basicclienthandler.java

📁 一个用java编写的服务器,对于学习网络编程的人来说是个很好的例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		return data;
	}

	/**
	 * Read the byte input. This will block till some data is
	 * received from the stream. Allowed only when 
	 * <code>DataType.IN</code> is in <code>DataMode.BYTE</code> mode.
	 * @return The data as a String
	 * @since 1.3.2
	 */
	public String readBytes() throws IOException {
		if(dataModeIN != DataMode.BYTE)
				throw new IllegalStateException("Can't read Byte: " + 
					"DataType.IN is not in DataMode.BYTE");
		byte data[] = readInputStream();
		if(data!=null)
			return new String(data, charset);
		else
			return null;
	}

	/**
	 * Sets the communication logging flag.
	 * @see #getCommunicationLogging
	 * @since 1.3.2
	 */
	public void setCommunicationLogging(boolean communicationLogging) {
		this.communicationLogging = communicationLogging;
	}
	/**
	 * Returns the communication logging flag.
	 * @see #setCommunicationLogging
	 * @since 1.3.2
	 */
	public boolean getCommunicationLogging() {
		return communicationLogging;
	}

	/**
	 * Returns the date/time when the client socket last sent a data to this
	 * ClientHanlder. If no client is currently connected it will return
	 * <code>null</code>
	 * @since 1.3.3
	 */
	public Date getLastCommunicationTime() {
		return lastCommunicationTime;
	}

	/**
	 * Updates the last communication time for this client
	 * @since 1.3.3
	 */
	public void updateLastCommunicationTime() {
		lastCommunicationTime = new Date();
	}

	/**
	 * Force the closing of the client by closing the associated socket.
	 * @since 1.3.3
	 */
	public synchronized void forceClose() throws IOException {
		if(getSelectionKey()!=null) getSelectionKey().cancel();
		if(getSocketChannel()!=null) {
			getSocketChannel().close();
			setSocketChannel(null);
		}
		if(getSocket()!=null) {
			getSocket().close();
			setSocket(null);
		}
	}

	/**
	 * Returns flag indicating if the client is connected in secure mode 
	 * (SSL or TLS).
	 * @return secure flag
	 * @since 1.4.0
	 */ 
	public boolean isSecure() {
		return secure;
	}

	/**
	 * Sets flag indicating if the client is connected in secure mode 
	 * (SSL or TLS).
 	 * @param secure
	 * @since 1.4.0
	 */
	public void setSecure(boolean secure) {
		this.secure = secure;
	}

	/**
	 * Updates the InputStream and OutputStream for the ClientHandler for the 
	 * set Socket.
	 * @since 1.4.0
	 * @see #setSocket
	 */
	public abstract void updateInputOutputStreams() throws IOException;

	/**
	 * Makes current Client connection to secure protocol based on the 
	 * secure configuration set to the server. This method will just call 
	 * <code>makeSecure(false, false, true, null)</code>.
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @since 1.4.0
	 */
	public void makeSecure() throws IOException, NoSuchAlgorithmException, 
			KeyManagementException {
		makeSecure(false, false, true, null);
	}

	/**
	 * Makes current Client connection to secure protocol.
	 * This method will just call <code>makeSecure(false, false, true, protocol)</code>.
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @since 1.4.0
	 */
	public void makeSecure(String protocol) throws IOException, 
			NoSuchAlgorithmException, KeyManagementException {
		makeSecure(false, false, true, protocol);
	}

	/**
	 * Makes current Client connection to secure protocol.
	 * @param useClientMode falg if the socket should start its first handshake in "client" mode.
	 * @param needClientAuth flag if the clients must authenticate themselves.
	 * @param autoClose close the underlying socket when this socket is closed 
	 * @param protocol the standard name of the requested protocol. If <code>null</code> will use the protocol set in secure configuration of the server.
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @since 1.4.0
	 */
	public void makeSecure(boolean useClientMode, boolean needClientAuth, 
			boolean autoClose, String protocol) throws IOException, 
			NoSuchAlgorithmException, KeyManagementException {
		if(isSecure()==true) {
			throw new IllegalStateException("Client is already in secure mode!");
		}
		
		appLogger.fine("Making secure - Protocol: "+protocol+
			", Client: ["+getHostAddress()+"]");

		javax.net.ssl.SSLSocketFactory sslSf = getServer().getSSLSocketFactory(protocol);
		String host = getServer().getBindAddr().getHostAddress();
		if(host.equals("0.0.0.0")) host = InetAddress.getLocalHost().getHostAddress();
		SSLSocket newSocket = (SSLSocket) sslSf.createSocket(
			getSocket(), host, getServer().getPort(), autoClose);
		newSocket.setNeedClientAuth(needClientAuth);
		newSocket.setUseClientMode(useClientMode);
		setSocket(newSocket);
		setSecure(true);
		updateInputOutputStreams();		
	}

	/**
	 * Send a binary data to the connected client.
	 * If client is not connected it will just return.
	 * @since 1.4
	 * @exception IOException
	 *        if Socket IO Error or Socket was closed by the client.
	 */
	public void sendClientBinary(byte data[]) throws IOException {
		sendClientBinary(data, 0, data.length);
	}

	/**
	 * Send a binary data to the connected client.
	 * If client is not connected it will just return.
	 * @since 1.4.5
	 * @exception IOException
	 *        if Socket IO Error or Socket was closed by the client.
	 */
	public void sendClientBinary(byte data[], int off, int len) throws IOException {
		if(isConnected()) {
			if(dataModeOUT != DataMode.BINARY)
				throw new IllegalStateException("Can't send Binary :" + 
					"DataType.OUT is not in DataMode.BINARY");
			if(getCommunicationLogging()) {
				appLogger.fine("Sending ["+getHostAddress()+"] : "+MyString.getMemInfo(len));
			}
			b_out.write(data, off, len); 
			b_out.flush();
		} else {
			logger.warning("Client not connected.");
		}
	}

	/**
	 * Read the binary input. This will block till some data is
	 * received from the stream. Allowed only when 
	 * <code>DataType.IN</code> is in <code>DataMode.BINARY</code> mode.
	 * @return The data as a String
	 * @since 1.4
	 */
	public byte[] readBinary() throws IOException {
		if(dataModeIN != DataMode.BINARY)
				throw new IllegalStateException("Can't read Binary :" + 
					"DataType.IN is not in DataMode.BINARY");
		byte data[] = readInputStream();
		return data;
	}

	/**
     * Sets the ClientBinaryHandler class that interacts with 
	 * client sockets.
	 * @param handler fully qualified name of the class that 
	 * implements {@link ClientBinaryHandler}
	 * @since 1.4
     */
	protected void setClientBinaryHandler(ClientBinaryHandler handler) {
		clientBinaryHandler=handler;
	}

	/** 
	 * Returns client SelectionKey associated, if any. 
	 * @since 1.4.5
	 */
	public Logger getAppLogger() {
		return appLogger;
	}

	/**
     * Sets the client socket's timeout.
	 * @param time client socket timeout in milliseconds.
	 * @see #getTimeout
	 * @since 1.4.5
     */
	public void setTimeout(int time) {
		socketTimeout = time;
	}	
	/**
     * Returns the Client socket timeout in milliseconds.
	 * @see #setTimeout
	 * @since 1.4.5
     */
	public int getTimeout() {
		return socketTimeout;
	}

	/** 
	 * Checks if this client has the event. 
	 * @since 1.4.5
	 */
	public boolean hasEvent(ClientEvent event) {
		synchronized(clientEvents) {
			return clientEvents.contains(event);
		}
	}

	/** 
	 * Adds the ClientEvent. 
	 * @since 1.4.5
	 */
	public void addEvent(ClientEvent event) {
		synchronized(clientEvents) {
			unprocessedClientEvents.add(event);
			clientEvents.add(event);
		}
	}

	/** 
	 * Removes the ClientEvent. 
	 * @since 1.4.5
	 */
	public void removeEvent(ClientEvent event) {
		if(event==null) return;

		synchronized(clientEvents) {
			clientEvents.remove(event);
		}

		ClientEvent _clientEvent = (ClientEvent)threadEvent.get();
		if(_clientEvent!=null && _clientEvent==event) {
			threadEvent.set(null);
		}
		
	}

	/** 
	 * Returns threads current event for this client. 
	 * @since 1.4.5
	 */
	protected ClientEvent getThreadEvent() {
		return (ClientEvent)threadEvent.get();
	}

	/** 
	 * Sets message to be displayed when maximum connection reaches.
	 * @since 1.4.5
	 */
	public void setMaxConnectionMsg(String msg) {
		maxConnectionMsg = msg;
	}
	/**
	 * Returns message to be displayed to the client when maximum 
	 * connection reaches.
	 * @since 1.4.5
	 */
	public String getMaxConnectionMsg() {
		return maxConnectionMsg;
	}

	/** 
	 * Sets client socket channel associated, if any. 
	 * @since 1.4.5
	 */
	public abstract void setSocketChannel(SocketChannel socketChannel);
	/** 
	 * Returns client socket channel associated, if any. 
	 * @since 1.4.5
	 */
	public abstract SocketChannel getSocketChannel();

	/** 
	 * Sets client SelectionKey associated, if any. 
	 * @since 1.4.5
	 */
	public abstract void setSelectionKey(SelectionKey selectionKey);
	/** 
	 * Returns client SelectionKey associated, if any. 
	 * @since 1.4.5
	 */
	public abstract SelectionKey getSelectionKey();

	public boolean getWillClean() {
		return willClean;
	}

	/**
	 * Register OP_READ with the SelectionKey associated with the channel. If SelectionKey is
	 * not set then it registers the channel with the Selector.
	 * @since 1.4.5
	 */
	public abstract void registerForRead() throws IOException, 
		ClosedChannelException;
	
	/**
	 * Register OP_WRITE with the SelectionKey associated with the channel.
	 * @since 1.4.5
	 */
	public abstract void registerForWrite() throws IOException, 
		ClosedChannelException;

	/**
     * Sets the ClientWriteHandler class that interacts with 
	 * client sockets.
	 * @param handler fully qualified name of the class that 
	 * implements {@link ClientWriteHandler}
	 * @since 1.4.5
     */
	protected abstract void setClientWriteHandler(ClientWriteHandler handler);

	/**
     * Sets the Charset to be used for String decoding and encoding.
	 * @param charset to be used for String decoding and encoding
	 * @see #getCharset
	 * @since 1.4.5
     */
	public void setCharset(String charset) {
		if(charset==null || charset.trim().length()==0)
			return;
		this.charset = charset;
	}
	/**
     * Returns Charset to be used for String decoding and encoding..
     * @see #setCharset
	 * @since 1.4.5
     */
	public String getCharset() {
		return charset;
	}

	/**
	 * Returns cached socket host ip address.
	 * @since 1.4.5
	 */
	public String getHostAddress() {
		return hostAddress;
	}

	protected void assertionSystemExit() {
		logger.warning("[Assertions Was Enabled] Forcing program exit to help developer.");
		org.quickserver.net.qsadmin.QSAdminShell.tryFullThreadDump();//it can help debug.
		try {
			Thread.sleep(100);	
		} catch(InterruptedException e) {
			logger.fine("Interrupted: "+e);
		}		
		System.exit(-1);
	}

	/**
	 * Checks if the passed ClientEvent is the one next for 
	 * processing if a thread is allowed through this object.
	 * @since 1.4.6
	 */
	public boolean isClientEventNext(ClientEvent clientEvent) {
		ClientEvent ce = null;
		synchronized(clientEvents) {
			if(unprocessedClientEvents.size()>0)
				ce = (ClientEvent) unprocessedClientEvents.get(0);
		}
		return clientEvent == ce;
	}

	/**
	 *Returns the {@link java.io.BufferedInputStream} associated with 
	 * the Client being handled. Can be null if not available at the time of method call. 
	 * @see #getBufferedOutputStream
	 * @since 1.4.6
	 */
	public BufferedInputStream getBufferedInputStream() {
		return b_in;
	}

	/**
	 * Returns the {@link java.io.BufferedOutputStream} associated with 
	 * the Client being handled. Can be null if not available at the time of method call. 
	 * @see #getBufferedInputStream
	 * @since 1.4.6
	 */
	public BufferedOutputStream getBufferedOutputStream() {
		return b_out;
	}

	protected void handleTimeout(SocketTimeoutException e) throws SocketException, IOException {
		appLogger.fine("Timeout - Client [" + getHostAddress() +"]");
		appLogger.finest("SocketTimeoutException : " + e.getMessage());

		String temp = null;
		if(clientExtendedEventHandler!=null) {
			clientExtendedEventHandler.handleTimeout(this);
		} else {
			temp = timeoutMsg;
			if(dataModeOUT == DataMode.STRING)
				temp = temp + NEW_LINE;
			if(dataModeOUT != DataMode.OBJECT) {
				out.write(temp.getBytes(charset));
				out.flush();
			}
			if(true) throw new SocketException("Timeout");
		}
	}
}

⌨️ 快捷键说明

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