android 连线 组件,Android的TCP Socket连接组件

package android.net.tcp;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.net.SocketAddress;

import java.net.UnknownHostException;

/**

* Socket连接操作类

*

* @author Esa

*/

public class SocketBase {

private Socket mSocket;// socket连接对象

private DataOutputStream out;

private DataInputStream in;// 输入流

private SocketCallback callback;// 信息回调接口

private int timeOut = 1000 * 30;

/**

* 构造方法传入信息回调接口对象

*

* @param sdi

*            回调接口

*/

public SocketBase(SocketCallback callback) {

this.callback = callback;

}

/**

* 连接网络服务器

*

* @throws UnknownHostException

* @throws IOException

*/

public void connect(String ip, int port) throws Exception {

mSocket = new Socket();

SocketAddress address = new InetSocketAddress(ip, port);

mSocket.connect(address, timeOut);// 连接指定IP和端口

if (mSocket.isConnected()) {

out = new DataOutputStream(mSocket.getOutputStream());// 获取网络输出流

in = new DataInputStream(mSocket.getInputStream());// 获取网络输入流

callback.connected();

}

}

public void setTimeOut(int timeOut) {

this.timeOut = timeOut;

}

/**

* 返回连接服是否成功

*

* @return

*/

public boolean isConnected() {

if(mSocket != null){

return mSocket.isConnected();

}

return false;

}

/**

* 发送数据

*

* @param buffer

*            信息字节数据

* @throws IOException

*/

public void write(byte[] buffer) throws IOException {

if (out != null) {

out.write(buffer);

out.flush();

}

}

/**

* 断开连接

*

* @throws IOException

*/

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对象

}

}

/**

* 读取网络数据

*

* @throws IOException

*/

public void read() throws IOException {

if (in != null) {

byte[] buffer = new byte[1024*1];// 缓冲区字节数组,信息不能大于此缓冲区

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;

}

}

}

}

package android.net.tcp;

import java.util.Vector;

/**

* 连接服务器线程类

*

* @author Esa

*/

public class SocketConnect implements Runnable {

private boolean isConnect = false;// 是否连接服务器

private boolean isWrite = false;// 是否发送数据

private static final Vector datas = new Vector();// 待发送数据队列

private SocketBase mSocket;// socket连接

private WriteRunnable writeRunnable;// 发送数据线程

private String ip = null;

private int port = -1;

/**

* 创建连接

*

* @param callback

*            回调接口

*/

public SocketConnect(SocketCallback callback) {

mSocket = new SocketBase(callback);// 创建socket连接

writeRunnable = new WriteRunnable();// 创建发送线程

}

@Override

public void run() {

if (ip == null || port == -1) {

throw new NullPointerException("not set address");

}

isConnect = true;

while (isConnect) {

synchronized (this) {

try {

mSocket.connect(ip, port);// 连接服务器

} catch (Exception e) {

try {

mSocket.disconnect();// 断开连接

this.wait(6000);

continue;

} catch (InterruptedException e1) {

continue;

}

}

}

isWrite = true;// 设置可发送数据

new Thread(writeRunnable).start();// 启动发送线程

try {

mSocket.read();// 获取数据

} catch (Exception e) {

e.printStackTrace();

} finally {

writeRunnable.stop();

mSocket.disconnect();

}

}

}

/**

* 关闭服务器连接

*/

public synchronized void disconnect() {

isConnect = false;

this.notify();

resetConnect();

}

/**

* 重置连接

*/

public void resetConnect() {

writeRunnable.stop();// 发送停止信息

mSocket.disconnect();

}

/**

* 向发送线程写入发送数据

*/

public void write(byte[] buffer) {

writeRunnable.write(buffer);

}

/**

* 设置IP和端口

*

* @param ip

* @param port

*/

public void setRemoteAddress(String host, int port) {

this.ip = host;

this.port = port;

}

/**

* 发送数据

*/

private boolean writes(byte[] buffer) {

try {

mSocket.write(buffer);

return true;

} catch (Exception e) {

resetConnect();

}

return false;

}

/**

* 发送线程

*

* @author Esa

*/

private class WriteRunnable implements Runnable {

@Override

public void run() {

System.out.println(">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) {

writes(buffer);// 发送数据

} else {

this.notify();

}

}

}

}

System.out.println(">TCP发送线程结束

}

/**

* 添加数据到发送队列

*

* @param buffer

*            数据字节

*/

public synchronized void write(byte[] buffer) {

datas.add(buffer);// 将发送数据添加到发送队列

this.notify();// 取消等待

}

public synchronized void stop() {

isWrite = false;

this.notify();

}

}

}

package android.net.tcp;

/**

* 获取网络数据回调类

*

* @author Esa

*

*/

public abstract interface SocketCallback {

/**

* 当建立连接是的回调

*/

public abstract void connected();

/**

* 当获取网络数据回调接口

*

* @param buffer

*            字节数据

*/

public abstract void receive(byte[] buffer);

/**

* 当断开连接的回调

*/

public abstract void disconnect();

}

//使用方法

SocketConnect connect = new SocketConnect(new SocketCallback() {

@Override

public void receive(byte[] buffer) {

System.out.println("Server Message :" + new String(buffer));

}

@Override

public void disconnect() {

}

@Override

public void connected() {

}

});

connect.setRemoteAddress("localhost", 11011);

new Thread(connect).start();

for(int i=0; i<100; i++){

connect.write("test".getBytes());

Thread.sleep(1000);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值