首页>代码>Android TCP Socket通信封装及使用例子>/TcpSocketCommunication/src/com/zhoudd/socket/SocketConnect.java
package com.zhoudd.socket;

import java.util.Vector;

import android.util.Log;

/** 
* @ClassName: SocketConnect 
* @Description: Socket TCP连接、发送、接收(回调)、断开连接 
* @author zhoudd 
* @date 2016年1月4日 下午1:16:20 
*  
**/
public class SocketConnect implements Runnable{

	private static final String TAG = "SocketConnect";
	
	/**
	 * 连接服务器失败 默认等待时间
	 */
	private static final int DISCONNECT_WAIT_TIME = 6*1000;
	
	private boolean isConnect = false;//是否连接了服务器
	private boolean isWrite = false;//是否发送数据
	private static final Vector<byte[]> datas = new Vector<byte[]>();//待发送数据队列
	private SocketBase socketBase;//socket连接
	private WriteRunnable writeRunnable;//发送数据线程
	
	private String ip = null;
	private int port = -1;
	
	
	/**
	 * 
	* <p>Title: 创建连接</p> 
	* <p>Description: </p> 
	* @param callback 回调接口
	 */
	public SocketConnect(SocketCallback callback){
		socketBase = new SocketBase(callback);//创建socket连接
		writeRunnable = new WriteRunnable();//创建发送线程
	}
	
	/**
	* @Description: 设置IP和端口 
	 */
	public void setRemoteAddress(String ip, int port){
		this.ip = ip;
		this.port = port;
	}
	
	
	
	@Override
	public void run() {
		if ( ip == null || port == -1 ){
			throw new NullPointerException("not set address");
		}
		
		isConnect = true;
		while ( isConnect ) {
			synchronized ( this ) {
				try {
					socketBase.connect(ip, port);//连接服务器
				} catch (Exception e) {
					Log.e(TAG, "socket发起连接异常,等待几秒重连", e);
					
					try {
						socketBase.disConnect();//断开连接
						this.wait(DISCONNECT_WAIT_TIME);
						continue;
					} catch (Exception e2) {
						e2.printStackTrace();
						continue;
					}
				}
			}
			
			isWrite = true;//设置可发送数据
			new Thread(writeRunnable).start();//启动发送线程
			try {
				socketBase.read();//获取数据
			} catch (Exception e) {
				e.printStackTrace();
			} 
//			finally{
//				writeRunnable.stop();
//				socketBase.disConnect();
//			}
		}
	}

	
	
	
	
	/**
	* @Description: 关闭服务器连接 
	 */
	public synchronized void disconnect(){
		isConnect = false;
		this.notify();
		writeRunnable.stop();//停止发送数据
		socketBase.disConnect();
	}
	

	
	/**
	* @Description: 向发送线程写入发送数据 ,发送线程发送出去
	* @param  buffer
	* @return void
	 */
	public boolean send(byte[] buffer){
		if (writeRunnable != null){
			writeRunnable.threadWrite(buffer);
			return true;
		}else{
        	return false;
		}
	}
	
	
	/**
	 * 
	* @Description: 发送数据 
	* @param  buffer
	* @return boolean
	 */
	private boolean socketWrite(byte[] buffer){
		try {
			return socketBase.write(buffer);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	
	
	
	
	
	
	/**
	* @ClassName: WriteRunnable 
	* @Description: 发送数据线程类 
	* @author W18992 
	* @date 2015年12月15日 下午7:37:17 
	 */
	private class WriteRunnable implements Runnable{

		@Override
		public void run() {
			Log.i(TAG, "---TCP 发送线程开启 ---");
			while ( isWrite ){
				synchronized (this) {
					if (datas.size() <= 0){
						try {
							this.wait();//等待发送数据
						} catch (InterruptedException e) {
							continue;
						}
					}
					while (datas.size() > 0) {
						byte[] buffer = datas.remove(0);//获取一条发送数据
						if ( isWrite ){
							socketWrite(buffer);//发送数据
						}else{
							this.notify();
						}
					}
				}
			}
			Log.i(TAG, "---TCP 发送线程结束 ---");
		}
		
		/**
         * 添加数据到发送队列
         * @param buffer 数据字节
         */
		public synchronized void threadWrite(byte[] buffer){
			datas.add(buffer);//将发送数据添加到发送队列
			this.notify();//取消等待
		}
		
		public synchronized void stop(){
			isWrite = false;
			this.notify();
		}
	}
}

 
最近下载更多
空中飞尘  LV13 3月2日
w719566673  LV1 1月16日
z843110987  LV1 2021年11月29日
zjh211  LV1 2021年5月24日
mrchen1  LV1 2020年9月29日
lzy6312  LV14 2020年8月30日
lgllllll  LV11 2020年6月15日
unp2018  LV8 2020年6月3日
AndrewLiuFuWen  LV9 2020年5月3日
qianyunjie  LV8 2020年4月20日
最近浏览更多
空中飞尘  LV13 3月2日
w719566673  LV1 1月16日
dzlwindy  LV8 2023年8月28日
fuyouou  LV5 2023年7月4日
tianli3000  LV7 2023年5月15日
Candycan 2023年5月4日
暂无贡献等级
臧家旺  LV3 2023年3月23日
Rucoding  LV7 2022年7月10日
bearloadprogress  LV7 2022年5月12日
zhw2016  LV5 2022年5月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友