tcpconnection.java

来自「短信发送」· Java 代码 · 共 86 行

JAVA
86
字号
/**
 * Created at Nov 22, 2008
 */
package com.jdev.net.connector;

import com.jdev.net.queue.QueueFactory;
import com.jdev.util.Debug;

/**
 * <p>Title: TcpConnection</p>
 * <p>Description: </p>
 * @author Lawrence
 * @version 
 */
public class TcpConnection extends Connection {

	private final static String module = TcpConnection.class.getName();
	private final static QueueFactory queueFactory = QueueFactory.getInstance();
	private boolean isConnect = false;
	private TcpClient client = null;

	public void attach(TcpClient client) {
		this.client = client;
	}
	
	/**
	 * @param type
	 */
	public TcpConnection(int type) {
		super(type);
		try{
			if (type == ConnectionFactory.CLIENT) {
				queue = queueFactory.getQueue(QueueFactory.TCP_QUEUE);
				this.CSType = ConnectionFactory.CLIENT;
	
			} else {
				queue = queueFactory.getQueue(QueueFactory.TCP_QUEUE);
				this.CSType = ConnectionFactory.TCPSERVER;	
				isConnect = true;
	
			}
		}catch(Exception e){
			
		}
	}

	/* (non-Javadoc)
	 * @see com.jdev.net.connector.Connection#close()
	 */
	@Override
	public void close() throws Exception {
		if (!isConnect)
			return;
		try {
			client.close();
			isConnect = false;
		} catch (Exception ex) {
			Debug.logError("-->TcpConnection close error:" + ex, module);
			throw new Exception(ex);
		}
	}

	/* (non-Javadoc)
	 * @see com.jdev.net.connector.Connection#isConnect()
	 */
	@Override
	public boolean isConnect() throws Exception {
		return isConnect;
	}

	/* (non-Javadoc)
	 * @see com.jdev.net.connector.Connection#open(java.lang.String, int)
	 */
	@Override
	public void open(String url, int port) throws Exception {
		try {
			client.openSocketChannel(url, port);
			isConnect = true;
		} catch (Exception ex) {
			Debug.logError("-->TcpConnection open error:" + ex, module);
			throw new Exception(ex);
		}
	}

}

⌨️ 快捷键说明

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