Tom Krcha's FlashRealtime

Hey amigo!
I am Tom Krcha, Gaming Evangelist at Adobe. These are my notes


Local Flash Peer-to-Peer Communication over LAN (without Cirrus/Stratus)

August 27th, 2010

native-ip-multicast-small Some of you guys were probably wondering how to establish P2P connections in the local network (LAN) without Adobe Cirrus. Yes. It’s possible. Using native IP-only multicast. Let’s build a simple chat with Posting. Adding Multicast broadcast is just a simple next step which I am sure you can accomplish by yourself with few hints from my article about Multicast

Open an IP Multicast connection. This can be done by specifying connection string as “rtmpf:”. Note, that this technique cannot be used for one-to-one communication. So no DIRECT_CONNECTIONS with NetStream, but you can do all RTMFP Group operations.

netConnection.connect("rtmfp:");

Once you get connected (on NetConnection.Connect.Success) you can set up a NetGroup or NetStream via GroupSpecifier. Then just set ipMulticastMemberUpdatesEnabled to true, which is responsible for establishing local connections between peers call addIPMulticastAddress with a multicast address and port. Multicast address should begin with 224 at least and port should be higher than 1024 – that means: “224.0.0.0:1024″, but the general rule is the higher the better = unique.

For my example below I’ve chosen rather higher numbers.

groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.addIPMulticastAddress("225.225.0.1:30303");

Together it will look like this:

private function connect():void{
	nc = new NetConnection();
	nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
	nc.connect("rtmfp:");
}
 
private function netStatus(event:NetStatusEvent):void{
	switch(event.info.code){
		case "NetConnection.Connect.Success":
			setupGroup();
			break;
	}
}
 
private function setupGroup():void{
	var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/groupOne");
	groupspec.postingEnabled = true;
	groupspec.ipMulticastMemberUpdatesEnabled = true;
	groupspec.addIPMulticastAddress("225.225.0.1:30303");
 
	group = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
	group.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
}

Try app

P2PChatLocal – open in two or more windows.

Download

You can download complete source code of a chat application here:
P2PChatLocal.mxml

Facebook comments:

24 Comments »

  1. Hi!

    It’s excelent!! In my work stratus is blocked and I can’t use this wonderfull feature, but, with this I think that there is light on the way for to use this in my application.

    Thanks a lot!

    Comment by andres — August 27, 2010 @ 5:36 pm

  2. netConnection.connect(“rtmpf:”);

    rtmpf –> rtmfp

    :)

    Comment by kingnare — August 28, 2010 @ 5:52 am

  3. Thanks for this blog post, very cool feature!

    Do you know if this local P2P over LAN is supported on FP10.1 in browser on Android? I don’t seem to be able to get that to work.

    Will try your code as an AIR for Android app to see if that does the trick.

    Comment by Peter — August 28, 2010 @ 12:48 pm

  4. @kingnare – oh, typo, thanks, fixed :) … @peter: yes, it works, just tried

    Comment by tom — August 28, 2010 @ 1:08 pm

  5. Tks! Really helful…

    Comment by changenylife — August 31, 2010 @ 9:01 am

  6. [...] 英文原文是这样的:http://www.flashrealtime.com/local-flash-peer-to-peer-communication-over-lan-without-stratus/ [...]

    Pingback by flash不连接stratus服务器能在局域网进行p2p : : 9RIA.Com 天地会博客聚合 — September 1, 2010 @ 7:41 am

  7. [...] project has two main applications in it. MulticastTest.mxml is example by fellow Adobe Platform Evangelist Tom Krcha. I modified the chat application into the [...]

    Pingback by @renaun posts: Multicast Messaging Example Using HydraP2P — September 7, 2010 @ 10:02 pm

  8. [...] Local Flash Peer-to-Peer Communication over LAN (from FlashRealtime.com) [...]

    Pingback by Flex Monkey Patches » Blog Archive » Rubbernecker’s Review – Chapter3 – Episode1 – (Adobe Flex/Flash/AIR/LCDS Blog post recap) — September 8, 2010 @ 5:44 pm

  9. Hi

    Is it possible to make P2P application (not in a LAN) without using Stratus ?

    Thks in advance
    Gilles

    Comment by Gilles — September 29, 2010 @ 4:33 pm

  10. Hi there,

    unfortunately, the connection does not establish here in my LAN. Any ideas what could be the reason ? I got the
    “NetConnection.Connect.Success
    NetGroup.Connect.Success”
    messages, but when I send something, nothing happens.

    Kind regards.

    Comment by codingBuddha — October 2, 2010 @ 8:24 pm

  11. [...] in a couple of weeks I had a play with the IP multicast feature in Flash today. Tom Krcha posted an excellent example on his blog a few months ago which demos this feature very [...]

    Pingback by Building A Multi-platform Flash Based Chat system | FlashNewz — October 8, 2010 @ 2:31 pm

  12. Thanks for the insight, really cool! just got one problem: using Burrito on a desktop AIR app, the connection closes everytime just after the connection. Here’s the trace:

    netStatus : NetConnection.Connect.Success > 30303
    netStatus : NetGroup.Connect.Success > 30303
    netStatus : NetGroup.Connect.Closed > 30303

    30303 is the port, tried different ones but same result…
    It seems to work on AIR for Android (also using Burrito).

    Comment by stef — October 31, 2010 @ 7:06 am

  13. There’s one problem. When you try to post the same message a few times then only the first one is sent, others are not. I fixed it adding random number as a parameter in message object (message.random = Math.random()). I don’t know if it’s a bug or not.

    Anyway. Thanks for such a great code.

    Comment by Michal Wroblewski — November 7, 2010 @ 2:38 am

  14. hi Michal, that’s not a bug. That’s a feature ;) This is there because of message forwarding in a P2P mesh, where message can go through couple of peers before it reaches final destination – so peers know that this specific message was already send/forwarded. Random number is a go or simply add sequence++ to make message unique.

    Comment by tom — November 7, 2010 @ 10:18 pm

  15. OK, if it’s a feature ;) But theoretically if there are lot of messages already sent then all of them need to be remembered by Flash Player and if new arrives then FP compares it to all previous messages? Or there’s more magic behind it?

    I’m currently watching your MAX session – great and deep overview on P2P, Thanks!

    Comment by Michal Wroblewski — November 7, 2010 @ 11:44 pm

  16. I guess, they are remember in a clever way – hash table or so. It’s definitely very quick and not demanding.

    Comment by tom — November 8, 2010 @ 6:54 am

  17. [...] Krcha的教程吧:) http://www.flashrealtime.com/local-flash-peer-to-peer-communication-over-lan-without-cirrus/ Read Comments [...]

    Pingback by Peter's Blog » 在局域网内创建Flash P2P连接 - All about Flash tech… — November 9, 2010 @ 5:58 pm

  18. [...] address and port for multicasting. You can find more details about multitasking for example here: http://www.flashrealtime.com (Tom Krcha – thanks about your excellent demos and work with Flash based [...]

    Pingback by PTM - Pasi Manninen - Web and Mobile Developer — November 18, 2010 @ 12:04 pm

  19. Hello Tom,

    Does it works on AIR for TVs ( running on Android)?
    Thanks

    Comment by Jhonny Everson — December 8, 2010 @ 2:42 pm

  20. Hey… thanks for the informative piece. Do you have any idea what Cirrus is sending over the wire that’s allowing non-native-IP connections? How hard do you think it would be to write a server that mimicked that?

    Comment by Josh Strike — December 22, 2010 @ 6:15 pm

  21. hey, its good…cant we send video or audio???

    Comment by Ashwini — December 28, 2010 @ 2:02 pm

  22. Hi Tom wanna ask you, How can i make this thing works when I was in a different computer ? anyway thanks for the tutorial.

    Comment by Herman — January 20, 2011 @ 1:03 pm

  23. This no longer seems to work.

    Comment by someguy — November 4, 2011 @ 4:18 am

  24. Hi Tom,
    thanks for this tuts, but its not working with me nothing is sent what should i do :(

    Comment by Wajdi azar — November 9, 2011 @ 8:06 pm

RSS feed for comments on this post. / TrackBack URL

Leave a comment

Comment moderation is enabled. Your comment may take some time to appear.