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

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;


/** 
* @ClassName: SocketBase 
* @Description:  socket tcp
* @author zhoudd 
* @date 2016年1月4日 下午1:15:10 
**/
public class SocketBase {

	private static final int BUF_SIZE = 1024*10;//一次接收缓冲区大小
	
	private Socket mSocket;//socket连接对象
	private DataOutputStream out;//socket连接对象
	private DataInputStream in;//输入流
	private SocketCallback callback;//信息回调接口
	
	private int timeout = 30*1000;
	
	 /**
     * @Description: 构造方法传入信息回调接口对象
     * @param callback 回调接口:各通信协议实现各自的回调接口
     */
	public SocketBase(SocketCallback callback){
		this.callback = callback;
	}
	
	public void setTimeOut(int timeout){
		this.timeout = timeout;
	}
	
	
	
	/**
	* @Description: 连接网络服务器 
	* @param  ip IP地址
	* @param  port 端口
	* @throws Exception
	 */
	public void connect(String ip, int port) throws Exception{
		mSocket = new Socket();
		mSocket.setTcpNoDelay(true);
		SocketAddress address = new InetSocketAddress(ip, port);
		mSocket.connect(address, timeout);
		if ( mSocket.isConnected() ){
			out = new DataOutputStream(mSocket.getOutputStream());//获取网络输出流
			in = new DataInputStream(mSocket.getInputStream());//获取网络输入流
			callback.connected();//连接上后的回调
		}
	}
	
	
	/**
	* @Description: 断开网络 
	* @return void
	 */
	public void disConnect(){
		try {
			if ( mSocket != null ) {
				if ( !mSocket.isInputShutdown() ){
					mSocket.shutdownInput();
				}
				if ( !mSocket.isOutputShutdown() ){
					mSocket.shutdownOutput();
				}
				if ( out != null ){
					out.close();
				}
				if ( in != null ){
					in.close();
				}
				mSocket.close();//关闭socket
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			callback.disconnect();//断开连接后的回调
			out = null;
			in = null;
			mSocket = null;//置空socket对象
		}	
	}
	
	
	/**
	* @Description: 发送数据 
	* @param  buffer 信息字节数据
	* @throws IOException
	 */
	public boolean write(byte[] buffer) throws IOException{
		if ( buffer == null || buffer.length <= 0 ){
			return false;
		}
		if ( out != null ){
			out.write(buffer);
			out.flush();
			return true;
		}else{
			return false;
		}
	}	
	
	/**
	* @Description: 读取网络数据 
	* @throws IOException
	 */
	public void read() throws IOException{
		if ( in != null ){
			byte[] buffer = new byte[BUF_SIZE];//缓冲区字节数组,信息不能大于此缓冲区
			byte[] tmpBuffer;// 临时缓冲区
			int len = 0;//读取长度
			
			while( (len = in.read(buffer)) > 0){
				tmpBuffer = new byte[len];//创建临时缓冲区
				System.arraycopy(buffer, 0, tmpBuffer, 0, len);//将数据拷贝到临时缓冲区
				callback.receive(tmpBuffer);//调用回调接口传入得到的数据--》进行处理
				tmpBuffer = null;
			}
		}
	}
}

 
最近下载更多
空中飞尘  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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友