实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.chenhl.testusbconnect;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.content.Context;
import android.util.Log;
import com.chenhl.testusbconnect.utils.FileHelper;
import com.chenhl.testusbconnect.utils.MyUtil;
public class ThreadReadWriterIOSocket implements Runnable
{
private Socket client;
private Context context;
public ThreadReadWriterIOSocket(Context context, Socket client)
{
this.client = client;
this.context = context;
}
@Override
public void run()
{
Log.d("chl", "a client has connected to server!");
BufferedOutputStream out;
BufferedInputStream in;
try
{
/* PC端发来的数据msg */
String currCMD = "";
out = new BufferedOutputStream(client.getOutputStream());
in = new BufferedInputStream(client.getInputStream());
androidService.ioThreadFlag = true;
while (androidService.ioThreadFlag)
{
try
{
if (!client.isConnected())
{
break;
}
/* 接收PC发来的数据 */
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "will read......");
/* 读操作命令 */
currCMD = readCMDFromSocket(in);
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "**currCMD ==== " currCMD);
/* 根据命令分别处理数据 */
if (currCMD.equals("1"))
{
out.write("OK".getBytes());
out.flush();
} else if (currCMD.equals("2"))
{
out.write("OK".getBytes());
out.flush();
} else if (currCMD.equals("3"))
{
out.write("OK".getBytes());
out.flush();
} else if (currCMD.equals("4"))
{
/* 准备接收文件数据 */
try
{
out.write("service receive OK".getBytes());
out.flush();
} catch (IOException e)
{
e.printStackTrace();
}
/* 接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据 */
byte[] filelength = new byte[4];
byte[] fileformat = new byte[4];
byte[] filebytes = null;
/* 从socket流中读取完整文件数据 */
filebytes = receiveFileFromSocket(in, out, filelength, fileformat);
// Log.v(Service139.TAG, "receive data =" new
// String(filebytes));
try
{
/* 生成文件 */
File file = FileHelper.newFile("ucliulanqi.apk");
FileHelper.writeFile(file, filebytes, 0, filebytes.length);
} catch (IOException e)
{
e.printStackTrace();
}
} else if (currCMD.equalsIgnoreCase("exit"))
{
out.write("exit ok".getBytes());
out.flush();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
out.close();
in.close();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (client != null)
{
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "client.close()");
client.close();
}
} catch (IOException e)
{
Log.e(androidService.TAG, Thread.currentThread().getName() "---->" "read write error333333");
e.printStackTrace();
}
}
}
/**
* 功能:从socket流中读取完整文件数据
*
* InputStream in:socket输入流
*
* byte[] filelength: 流的前4个字节存储要转送的文件的字节数
*
* byte[] fileformat:流的前5-8字节存储要转送的文件的格式(如.apk)
*
* */
public static byte[] receiveFileFromSocket(InputStream in, OutputStream out, byte[] filelength, byte[] fileformat)
{
byte[] filebytes = null;// 文件数据
try
{
in.read(filelength);// 读文件长度
int filelen = MyUtil.bytesToInt(filelength);// 文件长度从4字节byte[]转成Int
String strtmp = "read file length ok:" filelen;
out.write(strtmp.getBytes("utf-8"));
out.flush();
filebytes = new byte[filelen];
int pos = 0;
int rcvLen = 0;
while ((rcvLen = in.read(filebytes, pos, filelen - pos)) > 0)
{
pos = rcvLen;
}
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "read file OK:file size="
filebytes.length);
out.write("read file ok".getBytes("utf-8"));
out.flush();
} catch (Exception e)
{
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "receiveFileFromSocket error");
e.printStackTrace();
}
return filebytes;
}
/* 读取命令 */
public String readCMDFromSocket(InputStream in)
{
int MAX_BUFFER_BYTES = 2048;
String msg = "";
byte[] tempbuffer = new byte[MAX_BUFFER_BYTES];
try
{
int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
msg = new String(tempbuffer, 0, numReadedBytes, "utf-8");
tempbuffer = null;
} catch (Exception e)
{
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "readFromSocket error");
e.printStackTrace();
}
// Log.v(Service139.TAG, "msg=" msg);
return msg;
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)