通过socket获取字节流处理时最初使用的是BufferedReader和PrintWriter 这种方式在解析字符串时是比较方便的 但是在处理字节时不够方便最终还是回归到InputStream和OutputStream方式 不使用缓存直接使用字节流操作,一次传输的字节数据在300字节以内,目前没有测试差距会有多大。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.Arrays; import android.content.Context; import android.content.SharedPreferences; import android.os.Handler; import android.os.Message; import android.util.Log; public class SocThread extends Thread { private String ip = "10.0.0.113"; private int port = 6666; private String TAG = "socket thread"; private int timeout = 10000; int maxsize = 256; public Socket client = null; PrintWriter out; BufferedReader in; public boolean isRun = true; Handler inHandler; Handler outHandler; Context ctx; private String TAG1 = "===Send==="; SharedPreferences sp; public SocThread(Handler handlerin, Handler handlerout, Context context) { inHandler = handlerin; outHandler = handlerout; ctx = context; MyLog.i(TAG, "创建线程socket"); } /** * 连接socket服务器 */ public void conn() { try { initdate(); Log.i(TAG, "连接中……"); client = new Socket(ip, port); client.setSoTimeout(timeout);// 设置阻塞时间 MyLog.i(TAG, "连接成功"); in = new BufferedReader(new InputStreamReader( client.getInputStream())); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( client.getOutputStream())), true); MyLog.i(TAG, "输入输出流获取成功"); } catch (UnknownHostException e) { MyLog.i(TAG, "连接错误UnknownHostException 重新获取"); e.printStackTrace(); conn(); } catch (IOException e) { MyLog.i(TAG, "连接服务器io错误"); e.printStackTrace(); } catch (Exception e) { MyLog.i(TAG, "连接服务器错误Exception" + e.getMessage()); e.printStackTrace(); } } public void initdate() { sp = ctx.getSharedPreferences("SP", ctx.MODE_PRIVATE); ip = sp.getString("ipstr", ip); port = Integer.parseInt(sp.getString("port", String.valueOf(port))); MyLog.i(TAG, "获取到ip端口:" + ip + ";" + port); } /** * 实时接受数据 */ @Override public void run() { MyLog.i(TAG, "线程socket开始运行"); conn(); MyLog.i(TAG, "1.run开始"); String line = ""; while (isRun) { try { if (client != null) { MyLog.i(TAG, "2.检测数据"); // 以下是按行直接读取字符流 while ((line = in.readLine()) != null) { MyLog.i(TAG, "3.getdata" + line + " len=" + line.length()); MyLog.i(TAG, "4.start set Message"); Message msg = inHandler.obtainMessage(); msg.obj = line; inHandler.sendMessage(msg);// 结果返回给UI处理 MyLog.i(TAG1, "5.send to handler"); } } else { MyLog.i(TAG, "没有可用连接"); conn(); } } catch (Exception e) { MyLog.i(TAG, "数据接收错误" + e.getMessage()); e.printStackTrace(); } } } /** * 发送数据 * * @param mess */ public void Send(String mess) { try { if (client != null) { MyLog.i(TAG1, "发送" + mess + "至" + client.getInetAddress().getHostAddress() + ":" + String.valueOf(client.getPort())); out.println(mess);//按字符流发送 out.flush(); MyLog.i(TAG1, "发送成功"); Message msg = outHandler.obtainMessage(); msg.obj = mess; msg.what = 1; outHandler.sendMessage(msg);// 结果返回给UI处理 } else { MyLog.i(TAG, "client 不存在"); Message msg = outHandler.obtainMessage(); msg.obj = mess; msg.what = 0; outHandler.sendMessage(msg);// 结果返回给UI处理 MyLog.i(TAG, "连接不存在重新连接"); conn(); } } catch (Exception e) { MyLog.i(TAG1, "send error"); e.printStackTrace(); } finally { MyLog.i(TAG1, "发送完毕"); } } /** * 关闭连接 */ public void close() { try { if (client != null) { MyLog.i(TAG, "close in"); in.close(); MyLog.i(TAG, "close out"); out.close(); MyLog.i(TAG, "close client"); client.close(); } } catch (Exception e) { MyLog.i(TAG, "close err"); e.printStackTrace(); } } }
原始字节流读取处理如下
package com.example.testsocket; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.content.Context; import android.content.SharedPreferences; import android.os.Handler; import android.os.Message; import android.util.Log; public class SocThread extends Thread { private String ip = "10.0.0.113"; private int port = 6666; private String TAG = "socket thread"; private int timeout = 10000; private int maxsize = 256;// 默认接受大小 public Socket client = null; // PrintWriter out; // BufferedReader in; OutputStream out; InputStream in; public boolean isRun = true; Handler inHandler; Handler outHandler; Context ctx; private String TAG1 = "===Send==="; SharedPreferences sp; public SocThread(Handler handlerin, Handler handlerout, Context context) { inHandler = handlerin; outHandler = handlerout; ctx = context; MyLog.i(TAG, "创建线程socket"); } /** * 连接socket服务器 */ public void conn() { try { initdate(); Log.i(TAG, "连接中……"); client = new Socket(ip, port); client.setSoTimeout(timeout);// 设置阻塞时间 MyLog.i(TAG, "连接成功"); in = client.getInputStream(); out = client.getOutputStream(); // 以下在字符流时比较好用 // in = new BufferedReader(new InputStreamReader( // client.getInputStream())); // out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( // client.getOutputStream())), true); MyLog.i(TAG, "输入输出流获取成功"); } catch (UnknownHostException e) { MyLog.i(TAG, "连接错误UnknownHostException 重新获取"); e.printStackTrace(); conn(); } catch (IOException e) { MyLog.i(TAG, "连接服务器io错误"); e.printStackTrace(); } catch (Exception e) { MyLog.i(TAG, "连接服务器错误Exception" + e.getMessage()); e.printStackTrace(); } } public void initdate() { sp = ctx.getSharedPreferences("SP", ctx.MODE_PRIVATE); ip = sp.getString("ipstr", ip); port = Integer.parseInt(sp.getString("port", String.valueOf(port))); MyLog.i(TAG, "获取到ip端口:" + ip + ";" + port); } /** * 实时接受数据 */ @Override public void run() { MyLog.i(TAG, "线程socket开始运行"); conn(); MyLog.i(TAG, "1.run开始"); String line = ""; while (isRun) { try { if (client != null) { MyLog.i(TAG, "2.检测数据"); byte[] temp = new byte[maxsize];// 默认最带为256 int size = 0; while ((size = in.read(temp)) > 0) { // -1表示文件结尾 byte[] res = new byte[size];// 默认最带为256 MyLog.i(TAG, "4.start set Message 字节读取"); System.arraycopy(temp, 0, res, 0, size); for (int i = 0; i < size; i++) { line += res[i] + " "; } MyLog.i(TAG, "3.getdata " + line + " size=" + size); MyLog.i(TAG, "4.start set Message"); Message msg = inHandler.obtainMessage(); msg.obj = line; inHandler.sendMessage(msg);// 结果返回给UI处理 MyLog.i(TAG1, "5.send to handler"); } } else { MyLog.i(TAG, "没有可用连接"); conn(); } } catch (Exception e) { MyLog.i(TAG, "数据接收错误" + e.getMessage()); e.printStackTrace(); } } } /** * 发送数据 * * @param mess */ public void Send(byte[] mess) { try { if (client != null) { MyLog.i(TAG1, "发送" + mess + "至" + client.getInetAddress().getHostAddress() + ":" + String.valueOf(client.getPort())); out.write(mess);//按字节发送 out.flush(); MyLog.i(TAG1, "发送成功"); Message msg = outHandler.obtainMessage(); msg.obj = mess; msg.what = 1; outHandler.sendMessage(msg);// 结果返回给UI处理 } else { MyLog.i(TAG, "client 不存在"); Message msg = outHandler.obtainMessage(); msg.obj = mess; msg.what = 0; outHandler.sendMessage(msg);// 结果返回给UI处理 MyLog.i(TAG, "连接不存在重新连接"); conn(); } } catch (Exception e) { MyLog.i(TAG1, "send error"); e.printStackTrace(); } finally { MyLog.i(TAG1, "发送完毕"); } } /** * 关闭连接 */ public void close() { try { if (client != null) { MyLog.i(TAG, "close in"); in.close(); MyLog.i(TAG, "close out"); out.close(); MyLog.i(TAG, "close client"); client.close(); } } catch (Exception e) { MyLog.i(TAG, "close err"); e.printStackTrace(); } } }
参考: