Android Socket编程的基类编写

来源:互联网 发布:武林外传 知乎精华 编辑:程序博客网 时间:2024/06/07 20:27

摘要

由于最近的工作需求涉及到网络编程的Socket通信,所以自己编写了一个基类来实现基础的连接socket、断开连接、读操作和写操作。

代码实现

一个抽象类Connection,一个继承Connection的封装工具类WifiConnect

Connection.java

public abstract class Connection {    public static int CONNECT_STATE_NONE = 0;    public static int CONNECT_STATE_CONNECTING = 1;    public static int CONNECT_STATE_CONNECTED = 2;    private Context mContext = null;    public Connection(Context context) {        this.mContext = context;    }    public Context getContext() {        return mContext;    }    public abstract boolean connect();    public abstract void close();    /**     * @param buffer     * @param byteOffset     * @param byteCount     * @return     * @throws IOException     */    public abstract int read(byte[] buffer, int byteOffset, int byteCount) throws IOException;    /**     * @param buffer     * @param offset     * @param count     * @throws IOException     */    public abstract void write(byte[] buffer, int offset, int count) throws IOException;}

WifiConnect.java

public class WifiConnect extends Connection {    private static final String TAG = WifiConnect.class.getName();    /**     * 连接超时时长设置     */    private static final int CONN_TIME_OUT = 3000; //3s    private Socket mSocket;    private InputStream mInputStream;    private OutputStream mOutputStream;    private final Object mConnectLock = new Object();    private int mConnectState = CONNECT_STATE_NONE;    private String mAddress; //IP地址    private int mPort; //端口号    public WifiConnect(Context context) {        super(context);        this.mAddress = ""; //例如:192.168.0.1        this.mPort = 0; //例如:8080    }    /**     * 开启socket连接     *     * @return     */    @Override    public boolean connect() {        synchronized (mConnectLock) {            if (mConnectState == CONNECT_STATE_CONNECTED) {                LogHelper.w(TAG, "Connection exist");                return true;            } else if (mConnectState == CONNECT_STATE_CONNECTING) {                throw new IllegalStateException("cannot call connect while connecting");            } else {                mConnectState = CONNECT_STATE_CONNECTING;            }        }        LogHelper.w(TAG, "Connecting...");        Socket socket = null;        try {            socket = new Socket();            socket.connect(new InetSocketAddress(mAddress, mPort), CONN_TIME_OUT);            synchronized (mConnectLock) {                if (mConnectState == CONNECT_STATE_NONE) {                    mSocket = null;                    throw new IOException("socket close by user");                }            }            this.mSocket = socket;            this.mInputStream = mSocket.getInputStream();            this.mOutputStream = mSocket.getOutputStream();            synchronized (mConnectLock) {                mConnectState = CONNECT_STATE_CONNECTED;            }            return true;        } catch (Exception e) {            synchronized (mConnectLock) {                mConnectState = CONNECT_STATE_NONE;            }            Log.e(TAG, e.getMessage());            if (socket != null) {                try {                    socket.close();                } catch (Exception ex) {                }            }            return false;        }    }    /**     * 关闭socket连接     */    @Override    public void close() {        LogHelper.w(TAG, "WifiConnect socket close.");        if (mInputStream != null) {            try {                mInputStream.close();            } catch (Exception e) {                e.printStackTrace();            }            mInputStream = null;        }        if (mOutputStream != null) {            try {                mOutputStream.close();            } catch (Exception e) {                e.printStackTrace();            }            mOutputStream = null;        }        if (mSocket != null) {            try {                mSocket.close();            } catch (IOException e) {                e.printStackTrace();            }            mSocket = null;        }    }    @Override    public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {        LogHelper.d(TAG, "read: " + new String(buffer));        if (mInputStream != null) {            return mInputStream.read(buffer, byteOffset, byteCount);        } else {            throw new IOException("WifiConnect read IOException");        }    }    @Override    public void write(byte[] buffer, int offset, int count) throws IOException {        LogHelper.d(TAG, "write: " + new String(buffer));        if (mOutputStream != null) {            mOutputStream.write(buffer, offset, count);        } else {            throw new IOException("WifiConnect write IOException");        }    }}

mConnectLock 的作用:不让连接动作重复进行。

0 0
原创粉丝点击