由于项目的需要,需要在手机平台上面进行传输,但是,由于android平台的开发环境比较容易使用,而且免费,同时,因为在编写数据包的时候,已经在数据包中加入了发送数据包和接收数据包的函数,因此,考虑到开发投入的情况下,选择了使用Android平台。
由于在Android的应用大部分是使用Java进行开发的,而在Android中,并没有特殊的TCP的API,Android使用的TCP就是标准的Java的TCP的类,因此对于Android客户端来说,只需要将服务器版本的数据包代码中使用到TCP进行传输的代码,也就是Transfer.java进行简单的修改即可。

具体的代码如下:

/** *  */package edu.pkusz.sheng;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.net.Socket;/** * @author sheng * */public class Transfer{private static Socket ClientSocket = null;private static BufferedOutputStream SocketOutputStream = null;private static BufferedInputStream  SocketInputStream  = null;private static void CreateSocket(){try {ClientSocket = new Socket(CONSTANT.IP, CONSTANT.PORT);}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static void CreateSocketOutputStream(){if (ClientSocket == null){CreateSocket();}try {SocketOutputStream = new BufferedOutputStream(ClientSocket.getOutputStream());}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static void CreateSocketInputStream(){if (ClientSocket == null){CreateSocket();}try {SocketInputStream = new BufferedInputStream(ClientSocket.getInputStream());}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static boolean SendPackage(DataPackage PackageOfData){if (SocketOutputStream == null){CreateSocketOutputStream();}return PackageOfData.Send(SocketOutputStream);}public static boolean SendPackage(BufferedInputStream InputFile, PackageType Type, int LenOfData) throws IOException{final int LENOFCONTENT = BufferSize.BUFFERSIZE - BufferSize.PACKAGEHEADERSIZE;// The number of packages.int NumberOfPackages = LenOfData / LENOFCONTENT;if ((LenOfData % LENOFCONTENT) != 0){NumberOfPackages++;}DataPackage PackageOfData = new DataPackage(false);// set the type and the number of packages.PackageOfData.SetType(Type);PackageOfData.SetNumberOfPackages(NumberOfPackages);boolean result = false;byte[] TmpBuffer = new byte[LENOFCONTENT];int Len = 0;int IndexOfPackages = 1;while ((Len = InputFile.read(TmpBuffer)) != -1){PackageOfData.SetIndexOfPackages(IndexOfPackages);PackageOfData.SetSizeOfData(Len);// set contentPackageOfData.SetContent(TmpBuffer);// send dataresult = SendPackage(PackageOfData);IndexOfPackages++;}return result;}public static boolean SendString(PackageType Type, String InputString){if (InputString.length() > BufferSize.BUFFERSIZE){return false;}DataPackage PackageOfData = new DataPackage(false);// set the package headerPackageOfData.SetType(Type);PackageOfData.SetNumberOfPackages(1);PackageOfData.SetIndexOfPackages(1);PackageOfData.SetSizeOfData(InputString.length());// set the contentPackageOfData.SetContent(InputString.getBytes());return SendPackage(PackageOfData);}public static boolean SendUserName(String UserName){PackageType Type = PackageType.USERNAME;return SendString(Type, UserName);}public static boolean SendAuthorisztionRequest(){PackageType Type = PackageType.REQUESTAUTHORIZATION;String Request = new String("RequestAuthorization");return SendString(Type, Request);}public static boolean SendRequest(){PackageType Type = PackageType.REQUESTRESULT;String Request = new String("Request");return SendString(Type, Request);}public static boolean SendTextRequest(){PackageType Type = PackageType.REQUESTTEXT;String Request = new String("TextRequest");return SendString(Type, Request);}public static boolean SendClosedRequest(){PackageType Type = PackageType.CLOSED;String Request = new String("Closed");boolean result = SendString(Type, Request);try {result = Close();}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}public static boolean SendFile(String FileName) throws IOException{File RawFile = null;BufferedInputStream InputFile = null;try {RawFile = new File(FileName);InputFile = new BufferedInputStream(new FileInputStream(RawFile));}catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}int Len = (int) RawFile.length();PackageType Type = PackageType.FILEDATA;boolean result = false;result = SendPackage(InputFile, Type, Len);InputFile.close();return result;}public static boolean Close() throws IOException{if (SocketInputStream != null){SocketInputStream.close();}if (SocketOutputStream != null){SocketOutputStream.close();}if (ClientSocket != null){ClientSocket.close();}return true;}public static String ReceiveString(PackageType Type){DataPackage Data = new DataPackage(true);boolean run = true;if (SocketInputStream == null){CreateSocketInputStream();}while (run){int Len = 0;if ((Len = Data.Receive(SocketInputStream)) != -1){if (Data.GetType() == Type){run = false;}}}return new String(Data.Content, 0, Data.SizeOfData);}public static String ReceiveText(){PackageType Type = PackageType.REPLAYTEXT;return ReceiveString(Type);}public static String ReceiveUserName(){PackageType Type = PackageType.USERNAME;return ReceiveString(Type);}public static String ReceiveAuthorizen(){PackageType Type = PackageType.REPLAYAUTHORIZATION;return ReceiveString(Type);}public static String ReceiveResult(){PackageType Type = PackageType.REPLAYRESULT;return ReceiveString(Type);}}


更多相关文章

  1. Android/Linux内核驱动相关经典书籍大合集(Linux驱动工程师必备)
  2. 一个简易Android购物App的开发
  3. Android(安卓)App 安全的HTTPS 通信
  4. Android向导页面的实现
  5. 【iOS-cocos2d-X 游戏开发之十五】Cocos2dx中响应Android的Back
  6. 开源中国 4 周年, Android、iPhone、WP7三个平台客户端全面开源
  7. Android开发——消息处理传递机制
  8. Android(安卓)SDK工具(谷歌提供的16个工具)简介
  9. 《Android开发者必备知识体系 》写作计划

随机推荐

  1. 大华全景枪球联动控制失败问题排查
  2. 2020年,企业数字化转型的思考总结
  3. 2021-2-23:Java 文件映射内存是如何更新到
  4. 2021春节计划复盘-目标设定与任务管理
  5. Horizon虚拟桌面几例故障解决
  6. artipub发布测试
  7. PHP基础: 类的重载和命名空间的部分知识(
  8. zabbix报错:Error of query maxQueryMetri
  9. Heartbleed漏洞的原理与防治
  10. 数据可视化工具的部署环境和实现