Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7111720
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类:

2012-12-12 17:38:35

原文地址:通过alljoyn传输文件 作者:云少嘎嘎嘎

    Alljoyn是高通开发的一个开源的p2p通信框架,支持wifi/蓝牙和wifi-direct。目前已经有了很多基于这个框架开发的联机游戏。
    在Alljoyn的sdk里有很多例子,其中有一个通过raw session发送信息的,对这个例子略加修改就可以实现两个手机之间文件的传输。
     在client端,其发送的信息是通过一个FileOutputStream将要发送的信息写入到一个中间文件来发送到对方的。所以我们只要将要发送的文件通过流的形式输入到这个FileOutputStream中就可以了。

点击(此处)折叠或打开

  1. if (mStreamUp == true) {
  2.                     String string = (String) msg.obj + "\n";
  3.                     try {
  4.                         // logInfo(String.format("Writing %s to output stream",

  5.                         // string));

  6.                         // mOutputStream.write(string.getBytes());

  7.                         // logInfo(String.format("Flushing stream", string));

  8.                         // mOutputStream.flush();

  9.                         String filepath = "/sdcard/test.txt";
  10.                         File file = new File(filepath);
  11.                         InputStream in =new FileInputStream(file);
  12.                         byte[] buffer = new byte[1024];
  13.                         int read;
  14.                         long total = 0;
  15.                         while ((read = in.read(buffer)) != -1) {
  16.                             mOutputStream.write(buffer, 0, read);
  17.                             total+=read;
  18.                             Log.d("test","total:"+total);
  19.                             if(total>100000){
  20.                                 mOutputStream.flush();
  21.                                 total=0;
  22.                                 //容易出现io exception,所以稍微等待一下。

  23.                                 Thread.sleep(100);
  24.                             }
  25.                         }
  26.                         mOutputStream.flush();
  27.                         Log.d("test","flush");
  28.                         in.close();
  29.                     } catch (IOException ex) {
  30.                         Log.e("test",ex.toString());
  31.                     } catch (InterruptedException e) {
  32.                         // TODO Auto-generated catch block

  33.                         e.printStackTrace();
  34.                     }
  35.                 }
  36.                 break;
  37.             }
     其中前面注释掉的代码是实例程序原来的,下面的就是我修改的,将文件输入到输出流中。需要注意的是如果不加中间的sleep的话就会报IO Exception,这是因为raw使用的是非阻塞式socket发送的,如果发送得过快而网络传输得慢就会产生异常,中断传输。就好像一个大水池,一个水管往里注水,一个水管往外放水,如果注水的速度大于放水的速度的话水池就会溢出。所以在写入的时候稍微睡眠一下,也就是稍微控制一下注入的速度,让底层把内容发出去。
    虽然这样可以在一定程度上减少异常的发生,但是传输的文件如果太大的话还是容易产生异常的,在这种情况下可以使用try catch来捕获这个异常然后休眠一段时间从中断的位置重新传输。
 
    在server端,我们只需要将接受到的流写入到一个新建的文件中就可以了。其中注释掉的也是其原来的程序。

点击(此处)折叠或打开

  1. mReader = new Thread(new Runnable() {
  2.                         public void run() {
  3.                             logInfo("Thread running");
  4.                             Looper.myLooper().prepare();
  5.                             StringBuilder stringBuilder = new StringBuilder();
  6.                             try {
  7.                                 while (true) {
  8.                                     int c;
  9.                                     /*
  10.                                      * Wait until a character is available.
  11.                                      */
  12.                                     if (reader.ready() == false) {
  13.                                         Thread.sleep(100);
  14.                                         continue;
  15.                                     }
  16.                                     
  17.                                     /*
  18.                                      * Build a string out of the characters and
  19.                                      * when we see a newline, cook up a toast
  20.                                      * do display them.
  21.                                      */
  22.                                     if(saveFile("/mnt/sdcard/test.txt",is)){
  23.                                         Toast.makeText(Service.this, "save success", Toast.LENGTH_LONG).show();
  24.                                     }else{
  25.                                         Toast.makeText(Service.this, "save faild", Toast.LENGTH_LONG).show();
  26.                                     }
  27. //                     try {

  28. //                         c = reader.read();

  29. //                         logInfo(String.format("Read %d from stream", c));

  30. //                         stringBuilder.append((char)c);

  31. //                         if (c == 10) {

  32. //                             String s = stringBuilder.toString();

  33. //                         logInfo(String.format("Read %s from stream", s));

  34. //              Message toastMsg = mHandler.obtainMessage(MESSAGE_POST_TOAST, s);

  35. //              mHandler.sendMessage(toastMsg);

  36. //          stringBuilder.delete(0, stringBuilder.length() - 1);

  37. //                         }

  38. //                     } catch (IOException ex) {

  39. //                     }

  40.                                 }
  41.                             } catch (Throwable ex) {
  42.                                  logInfo(String.format("Exception reading raw Java stream: %s", ex.toString()));
  43.                             }
  44.                             logInfo("Thread exiting");
  45.                         }
  46.                     }, "reader");
  47.                     mReader.start();
    我们还需要加入一个保存文件的函数:

点击(此处)折叠或打开

  1. private static boolean saveFile(String filePath, InputStream is) {
  2.         boolean saved = false;
  3.         byte[] buffer = new byte[1024];
  4.         FileOutputStream fos = null;
  5.         File file = null;
  6.         try {
  7.             try {
  8.                 file = new File(filePath);
  9.                 fos = new FileOutputStream(file,true);
  10.             } catch (Exception e) {
  11.                 Log.e(TAG,"creat file error");
  12.             }
  13.             int readlen = -1;
  14.             long readCount = 0;
  15.             while (true) {
  16.                 saved = true;
  17.                 try {
  18.                     readlen = is.read(buffer);
  19.                 } catch (IOException e) {
  20.                      Log.e(TAG,"read inputstream error");
  21.                 } finally {
  22.                     if (readlen <= 0) {
  23.                         break;
  24.                     }
  25.                 }
  26.                 readCount += readlen;
  27.                 Log.d("test","FILE length::::::::::::::::::::"+readCount);
  28.                 try {
  29.                     fos.write(buffer, 0, readlen);
  30.                 } catch (IOException e) {
  31.                     Log.e(TAG,"write file error");
  32.                 }
  33.             }
  34.         } finally {
  35.             if (fos != null) {
  36.                 try {
  37.                     fos.close();
  38.                 } catch (IOException e) {
  39.                     Log.e(TAG, e.toString());
  40.                     }
  41.                 }
  42.                 fos = null;
  43.             }
  44.      Log.d("test","saved:"+saved);
  45.         return saved;
  46.     }

     好了,这样在client端的sd卡的根目录中新建一个test.txt文件,当两个手机连接起来后,client端随便发送一个字符串就可以触发传输文件的操作了。

 



 
 
 
 

 
 
 
阅读(399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~