一篇很不错的讲解"利用flash player 10.1中的p2p特性实现文件共享"的文章,为防止原文被墙掉,转载于此,原始出处:http://www.flashrealtime.com/file-share-object-replication-flash-p2p/

Object Replication

Object Replication is the most lowest-level P2P access available in Flash Player 10.1 (followed by Multicast, Posting and Directed Routing). It basically enables you to send chunks of data between peers. Object Replication is the only P2P access method that guarantees that all data will be transferred to all receiving peers.

Demo

I’ve built this simple file sharing application, which basically loads a file and then you start sharing it. Open another client to start receiving the file.

How to use it:
Open a provider in one window – browse for a file (JPG, PNG, GIF). Once it’s loaded, it will start sharing the file. Open a receiver in many other windows and start receiving. Provider and receiver are included in one app in this example.

How does it work

A classic scenario for Object Replication in Flash is file sharing. You have two clients, one is sending the data (Provider) and the other one receives the data (Receiver). You were able to do this already in Flash Player 10 using NetStream – but this worked only for two clients and there where no replication of objects to the members of a group => Massive File Sharing! In our scenario, you can have thousands of receivers.

Provider
Provides data for others. This is the originator. First you need to have an object with data you want to share. You most probably will load a file using URLStream or FileReference. Then you need to split this file into separate ByteArray chunks and give them indexes (it can be an indexed array). Keep the chunks reasonably small to avoid transfer issues (around 64KB). So if you load a 2 MB file, you will have 32 chunks. Finally call NetGroup.addHaveObject(0, 32); which says you have in this case 32 chunks available for others.

Receiver
Receives data from a provider. Here you just call NetGroup.addWantObjects(index, index); and start receiving objects from the provider. I do this by keeping the increasing the index by 1 once received a chunk. So you basically call NetGroup.addWantObjects(index, index); 32 times. When you call addWantObjects, the Providers gets a status “NetGroup.Replication.Request”. At this point the provider needs to write data to a group using NetGroup.writeRequestedObject(event.info.requestID,chunks[event.info.index]). Once it writes the data, the Receiver gets a “NetGroup.Replication.Fetch.Result” status event and save the data locally to an object. Remember, that the Receiver is just receiving data, it is not providing the data to other peers. After it has received all chunks, the Receiver just goes through the chunks and put them together into a final ByteArray.

Receiver/Provider
So why not to provide the received data to other peers to make the trasfer faster and maybe more stable. Once you receive some data using NetGroup.addWantObjects(index, index); and save them in “NetGroup.Replication.Fetch.Result”, you can start providing the data to other peers using NetGroup.addHaveObject(index, index);.

Of course there is lot more to be done, but first let’s have a look at this schema for the above.

Simple Object Replication

To demonstrate how Object Replication works, let’s try this second demo. In this example you have an object, which we fill with and array of 100 elements. Then start sharing this array. Run the second client to start receiving the array.

Provider peer:
1. Once connected, click fillObject
2. Then click addHaveObjects
3. That’s all

Receiver peer:
1. Once connected, click addWantObjects
2. It should start receiving objects shortly
(there is a loop, first chunk you receive is count of objects, once you receive a chunk the index increases by 1 and asks for next chunk until they are all received)

There are couple more buttons – you can try playing with it a little bit if you want.

How was it built?

Once connected to a server setup a NetGroup instance like this:

private function setupGroup():void{
  var spec:GroupSpecifier = new GroupSpecifier("myGroup");
  spec.serverChannelEnabled = true;
  spec.objectReplicationEnabled = true; 
  netGroup = new NetGroup(netConnection,spec.groupspecWithAuthorizations());
  netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
}

Once connected to a NetGroup, which means that user allowed P2P connections, UDP is enabled and so on – you do operations on a NetGroup. First set object replication strategy. We will be receiving packets one by one (moreless).

netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST;

In your netStatusHandler – catch two codes:

// This code is called on a Provider
case "NetGroup.Replication.Request": 
  // calling this causes "NetGroup.Replication.Fetch.Result" invocation on a Receiver
  netGroup.writeRequestedObject(event.info.requestID,obj[event.info.index]) 
  break; 
// This code is called on a Receiver
case "NetGroup.Replication.Fetch.Result":        
  // received chunks can be already provided to others
  netGroup.addHaveObjects(event.info.index,event.info.index);         
  // write a chunk into an object/array
  obj[event.info.index] = event.info.object;
   if(event.info.index == 0){
    // First chunk (0) holds the number of chunks
    objSize = Number(event.info.object);}
  else{
    // Receive chunks until you are full
    if(event.info.index+1<objSize){
      netGroup.addWantObjects(event.info.index+1,event.info.index+1);
      actualFetchIndex = event.info.index+1;
    }
  }
  break;

The whole source code can be found here.

Creating ByteArray P2P File Sharing

By following the concept above it’s possible to load a file from disk or url and then start sharing it with others.

This explains how the first demo works.

For this I’ve split the application into four different classes:

LocalFileLoader.as
Loads a file using FileReference and splits it into chunks (~64 KB each).

P2PFileShare.as
Connects to Cirrus and handles all Object Replication sending and receiving

P2PSharedObject.as
A simple value object, which holds the data (ByteArray), size, packetLenght, actualFetchIndex and chunks; it’s used by both classes above.

P2PFileSharing.mxml
The user interface, which puts it all together.

The complete source code can be found here.

The Provider should look like this after sending the data:

The Receiver should look like this after receiving the data:

In the next tutorial, I will look at how to use Object Replication with VOD video.

Where to go from here

Check other tutorials on P2P in Flash:

- Video-on-Demand over P2P in Flash Player 10.1 with Object Replication
- P2P GroupSpecifier Class Explained In Details Part 1
- Multicast Explained in Flash 10.1 P2P
- Directed Routing Explained in Flash 10.1 P2P
- Simple chat with P2P NetGroup in FP 10.1

Video tutorials:
- P2P Chat with NetGroup in Flash Player 10.1
- Multicast Streaming in Flash Player 10.1 Tutorial

[转载]Flash P2P 文件共享基础教程相关推荐

  1. [转载] Python3入门精通基础教程(合集)

    参考链接: Python3基础 Python3基础教程,持续更新中... 非常好的Python3基础教程合集,简单明了,快速掌握Python3核心技术! 01--Python及其开发环境的安装(Pyc ...

  2. 电脑入门基础教程_ARM入门最好的文章------转载一位资身工程师的入门心得

    开始学ARM一团雾水,不知道如何入手.这个介绍的比较全面.本人认为aRM入门最好的文章 1. 抓住51开发ARM 这几个月来我一直都爬在51的问题,自己都有一点笑自己了,用了4个月的时间,来巩固51的 ...

  3. [转载]VBS入门教程 VBS基础教程

    [转载]VBS入门教程 VBS基础教程(收藏) VBS基础教程 VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言. Microsoft Visual Basic是微软公司 ...

  4. [转载] Python OpenCV 基础教程

    参考链接: python opencv 基础5 : putText() 为图像增加文本 Python OpenCV基础教程 文章目录 Python `OpenCV`基础教程1. 简介2. 安装3. 使 ...

  5. Web前端-JavaScript基础教程上

    Web前端-JavaScript基础教程 将放入菜单栏中,便于阅读! JavaScript是web前端开发的编程语言,大多数网站都使用到了JavaScript,所以我们要进行学习,JavaScript ...

  6. 什么是HTML?HTML怎么学?HTML基础教程

    1.一般我都是在记事本中写HTML文件,也有很多人用DreamWeaver,这个随意~~ 2.HTML的一般结构如下: <html>----以<html>开始,以</ht ...

  7. 转载:P2P技术原理及应用(2)

    转载allen303allen的空间 在Gnutella网络中存在以下问题: 冗余消息多,对带宽的消耗存在一定的浪费.Gnutella网络协议采用泛洪式(Flooding)消息传播机制,这种消息传播机 ...

  8. 51自学网sketchup8基础教程 3dmax高级建模教程 VR产品级渲染教程 家具设计制造教程...

    我要自学网平面设计 计算机基础知识教程 Excel2010基础教程 Word2010基础教程 PPT2010基础教程 五笔打字视频教程  我要自学网Excel函数应用教程 Excel VBA基础教程 ...

  9. HTML入门基础教程相关知识

    HTML入门基础教程 html是什么,什么是html通俗解答: html是hypertext markup language的缩写,即超文本标记语言.html是用于创建可从一个平台移植到另一平台的超文 ...

最新文章

  1. android 自定义折线图
  2. [Android] 按钮单击事件的五种写法
  3. RabbitMQ 入门教程(PHP版) 第三部分:发布/订阅(Publish/Subscribe)
  4. 找工作的迷茫期开始了
  5. mysql定义变量字符串类型_mysqli_stmt :: bind_param():类型定义字符串中的元素数量与绑定变量的数量不匹配...
  6. JS基础入门篇(四)—this的使用,模拟单选框,选项卡和复选框
  7. 和could的区别用法_高考英语语法情态动词用法指南
  8. 美团技术leader:写给工程师的十条精进原则
  9. TCP 协议(包含三次握手,四次挥手)
  10. 爬虫入门(五):下载豆瓣电影信息
  11. 联想笔记本windows10,点击蓝牙显示无法连接
  12. Altium Designer快捷键,布线技巧
  13. linux(所有版本)下安装有道词典
  14. 一键刷入twrp_努比亚Z17-Z17S-Z17mini 刷入MIUI10系统刷机教程
  15. 迅雷种子为什么php文件后缀,迅雷BT文件后缀是什么?
  16. 错误:Attempted read from closed stream尝试读取关闭的流!!!
  17. 微信账号和语音文件的结构分析
  18. po,bo,vo,pojo,dto的区别
  19. 使用TensorFlow XLA辅助实现BERT预训练加速
  20. 颐 山雷颐 艮上震下

热门文章

  1. Facebook Messenger要点燃聊天机器人革命,据说四月就发布!
  2. 项目问题思考之策略模式
  3. Linux下如何使用虚拟用户增加FTP的安全性
  4. shiro学习总结(一)----初识shiro
  5. MobaXterm使用
  6. C#之Directory类、DirectoryInfo类和Fileinfo,File以及FilesSystemInfo
  7. [moka同学笔记]redis练习Demo
  8. FW 每秒百万级别的 HTTP 请求 sung: 重型的(heavy-duty)、分布式的、多协议测试工具...
  9. 看人装X,我就来气,开启极限装X模式
  10. [计算数学基础]矩阵微分