这是安卓端 连接服务器的子线程代码
public class SocThread extends Thread {
private String ip = "192.168.1.144";
private int port = 8000;
private String TAG = "socket thread";
private int timeout = 10000;
public Socket socket = null;
PrintWriter out;
BufferedReader in;
public boolean isRun = true;
Handler inHandler;
Context ctx;
public SocThread(Handler handlerin, Context context) {
inHandler = handlerin;
ctx = context;
}
/**
* 连接socket服务器
*/
public void conn() {
try {
Log.i(TAG, "连接中……");
socket = new Socket(ip, port);
socket.setSoTimeout(timeout);// 设置阻塞时间
// 发送数据包,默认为 false,即客户端发送数据采用 Nagle 算法;
// 但是对于实时交互性高的程序,建议其改为 true,即关闭 Nagle 算法,客户端每发送一次数据,无论数据包大小都会将这些数据发送出去
socket.setTcpNoDelay(true);
// 设置输出流的发送缓冲区大小,默认是4KB,即4096字节
socket.setSendBufferSize(4096);
// 作用:每隔一段时间检查服务器是否处于活动状态,如果服务器端长时间没响应,自动关闭客户端socket
// 防止服务器端无效时,客户端长时间处于连接状态
socket.setKeepAlive(true);
Log.i(TAG, "阻塞");
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
Log.i(TAG, "输入");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
Log.i(TAG, "输出");
} catch (UnknownHostException e) {
e.printStackTrace();
conn();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接
*/
@Override
public void run()
conn();
}
/**
* 发送数据
*
* @param mess
*/
public void Send(char mess) {
try {
if (socket != null) {
Log.i(TAG, "输出");
out.println(mess);
out.flush();
Log.i(TAG, "成功");
} else {
Log.i(TAG, "失败");
conn();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
/**
* 关闭连接
*/
public void close() {
try {
if (socket != null) {
in.close();
out.close();
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class SocThread extends Thread {
private String ip = "192.168.1.144";
private int port = 8000;
private String TAG = "socket thread";
private int timeout = 10000;
public Socket socket = null;
PrintWriter out;
BufferedReader in;
public boolean isRun = true;
Handler inHandler;
Context ctx;
public SocThread(Handler handlerin, Context context) {
inHandler = handlerin;
ctx = context;
}
/**
* 连接socket服务器
*/
public void conn() {
try {
Log.i(TAG, "连接中……");
socket = new Socket(ip, port);
socket.setSoTimeout(timeout);// 设置阻塞时间
// 发送数据包,默认为 false,即客户端发送数据采用 Nagle 算法;
// 但是对于实时交互性高的程序,建议其改为 true,即关闭 Nagle 算法,客户端每发送一次数据,无论数据包大小都会将这些数据发送出去
socket.setTcpNoDelay(true);
// 设置输出流的发送缓冲区大小,默认是4KB,即4096字节
socket.setSendBufferSize(4096);
// 作用:每隔一段时间检查服务器是否处于活动状态,如果服务器端长时间没响应,自动关闭客户端socket
// 防止服务器端无效时,客户端长时间处于连接状态
socket.setKeepAlive(true);
Log.i(TAG, "阻塞");
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
Log.i(TAG, "输入");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
Log.i(TAG, "输出");
} catch (UnknownHostException e) {
e.printStackTrace();
conn();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接
*/
@Override
public void run()
conn();
}
/**
* 发送数据
*
* @param mess
*/
public void Send(char mess) {
try {
if (socket != null) {
Log.i(TAG, "输出");
out.println(mess);
out.flush();
Log.i(TAG, "成功");
} else {
Log.i(TAG, "失败");
conn();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
/**
* 关闭连接
*/
public void close() {
try {
if (socket != null) {
in.close();
out.close();
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

