android简单的socket通讯

时间:2022-02-25 15:34:56

****socket 初始化 及心跳包线程
import android.os.Handler;
import android.os.Message;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
* Created by Administrator on 2016/6/7.
*/
public class SocketRunnable implements Runnable {
private boolean isRun = true;

private Socket socket;

private BufferedReader br;

private OutputStream ou;

private InetSocketAddress inet;
/**服务器 推送返回监听*/
private ResposeListener resposeListener;
/**上传数据 工具类*/
private Respose respose;

private Handler handler;
/**消息接收线程*/
private Thread thread;

private boolean isFirst = true;

public SocketRunnable() {
/**初始化 心跳包的 参数*/
respose = new Respose();
respose.setData("AAAAAAAAAAAAAAAA");
respose.setUid("1111");
respose.setCmd(1000);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
String str = (String) msg.obj;
resposeListener.Respose(str);
}
}
};
}

@Override
public void run() {
inet = new InetSocketAddress("192.168.1.15", 8888);
/**第一次是否连上*/
while (isFirst) {
try {
socket = new Socket();
socket.connect(inet, 5000);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ou = socket.getOutputStream();
isFirst = false;
} catch (IOException e) {
e.printStackTrace();
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}

/**开启子线程获取数据*/
thread = new Thread(new SocketBrRunnable(br, handler));
thread.start();
while (isRun) {
try {
send();
} catch (IOException e) {
reConnect();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

/**
* 发送心跳包
* @throws IOException
*/
public void send() throws IOException {
ou.write((GsonHelper.getHelper().getGson().toJson(respose) + "\r\n").getBytes("utf-8"));
}

/**
* 发送数据
* @param respose
* @throws IOException
*/
public void send(Respose respose) throws IOException {
ou.write((GsonHelper.getHelper().getGson().toJson(respose) + "\r\n").getBytes("utf-8"));
}

/**
* 发送文件
* @param respose
* @param file
* @throws IOException
*/
public void sendFile(Respose respose, File file) throws IOException {
final SocketFileThread fileThread = new SocketFileThread(respose, file);
fileThread.setRequestFileSucess(new RequestFileSucess() {
@Override
public void succress() {
if (!fileThread.isInterrupted()) {
try {
fileThread.interrupt();
fileThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
});
fileThread.start();
}

/**
* socket 重连
*/
private void reConnect() {
while (isServerClose() && isRun) {
try {
Thread.sleep(1000);
socket = new Socket();
socket.connect(inet, 5000);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ou = socket.getOutputStream();
stopBrThred();
/**开启子线程获取数据*/
thread = new Thread(new SocketBrRunnable(br, handler));
thread.start();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

/**
* 关闭获取 数据线程
*/
private void stopBrThred() {
if (!thread.isInterrupted()) {
try {
thread.interrupt();
thread.join();
thread = null;
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}

/**
* 通过发送数据 判断是否连上
* 发送1个字节的紧急数据,默认情况下,服务器端没有开启紧急数据处理,不影响正常通信
* @return
*/
private Boolean isServerClose() {
try {
socket.sendUrgentData(0);
return false;
} catch (Exception se) {
return true;
}
}

public void setResposeListener(ResposeListener resposeListener) {
this.resposeListener = resposeListener;
}

/**
* 清缓存
*/
public void destroy() {
isRun = false;
socket = null;
br = null;
stopBrThred();
handler = null;
resposeListener = null;
try {
ou.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

****socket 数据接收线程

import android.os.Handler;
import android.os.Message;

import java.io.BufferedReader;
import java.io.IOException;

/**
* Created by Administrator on 2016/6/8.
*/
public class SocketBrRunnable implements Runnable {

private BufferedReader br;

private Handler handler;

public SocketBrRunnable(BufferedReader br, Handler handler) {
this.br = br;
this.handler = handler;

}

@Override
public void run() {
String content;
/**不断的读取Socket输入流的内容*/
try {
while ((content = br.readLine()) != null) {
/**每当读取到来自服务器的数据之后,发送的消息通知程序*/
Message msg = new Message();
msg.what = 0x123;
msg.obj = content;
handler.sendMessage(msg);
}
} catch (IOException io) {
io.printStackTrace();
}
}

}