Adobe air’s networking issues and solutions.

If you only use tcp (socket) with adobe air.Congratulations, you will not meet these networking issues which will mentioned at below.
These issues are about UDP and broadcast (DatagramSocket).

1, The broadcast can not send data to “255.255.255.255”.
4 years past,the bug still exist. See this post:255.255.255.255 bug

“255.255.255.255” is whole Ip address range, but we can use “192.168.*.255” instead of “255.255.255.255”. “*” is the same IP address range as app’s.

2, Mac os don’t support broadcast!
see my post on adobe site:Mac AIR DatagramSocket can not support broadcast!
It’s very useful when discover new device in the network.
There are lot’s of smart devices in our home, like TV, air condition and so on!

As can not do it, but we can use other tools to resolve it.
I suggest node.js. Because node.js is cross platforms, it can work on windows and mac os. And it’s so easy to learn and use.

see my code here:

As3:

protected function broadcast(bt:ByteArray):void{
	try{
		if(os=="mac"){
			nodeProcess.discover(bt);
		}else{
			_discoverSocket.send(bt,0,0,localAdress.broadcast,_fixedBroadcastPort);//broad cast to player, use fixed port
		}
	}catch(e:Error){
	}
}

send data from air process to node process:

public function discover(bt:ByteArray):void{
	if(process.running){
		process.standardInput.writeBytes(bt);
	}else{
		trace("node is not running2>>>>>>>>>>>>>>")
		process.start(nativeProcessStartupInfo);
	}
}

send data by node js:

/**
 * User: yi
 * Date: 13-12-9
 */
var def = require('./hbms-def.js');
 var defPort=5321;
var dgram = require('dgram');
var udp=dgram.createSocket("udp4");
udp.on("error",function(e){
        console.log("udp error:"+ e.stack);
    });
    udp.on("message",function(msg,rinfo){
        console.log("server got: " + msg + " from " +
            rinfo.address + ":" + rinfo.port);
    });
    udp.on('listening', function () {
        udp.setBroadcast(true);
        console.log(">>>>>>>>>>>>>listening");
        onReady();
    });
bind();
function bind(){
    console.log(">>>>>>>>>>>>>bind"+defPort);
    udp.bind(defPort,null,function(error){
        if(error!=null){
             console.log("bind error>"+error);
            defPort++;
            bind();
        }
     });
}
function onReady(){
    process.stdout.write("onReady");
}
process.stdin.resume();
process.stdin.on('data', function(rs) {
    //var rs=result.trim();
     //console.log(">>rs"+rs.length+"<<"+rs.toString("hex"));
    udp.send(rs,0,rs.length,4999,"255.255.255.255",function(error){
        if(error!=null){
            console.log(">> send bytes error:"+ error);
        }
    });
});
process.stdin.on("error",function(error){
    console.log("udp process node "+error);
})

Don’t know node js? you can learn more at nodejs.org

You may also like...

2 Responses

  1. EZ says:

    This workaround didn’t work for me:
    “192.168.*.255″ instead of “255.255.255.255”
    Trying to send data from Android to PC (Windows).

    Using UDP DatagramSocket to send data to a specific address (192.168.2.150) works great, but changing it to 192.168.2.255 fails…
    Any idea?

  2. liuyi says:

    @EZ I only use Adobe air on desktop computer, only windows can send broadcast to 192.168.*.255.
    If it don’t work on Android, you can send broadcast by native (Java or C++ ) way.

    Any way, Flash almost to dead. It already became a HTML5 animation tools .

Leave a Reply

Your email address will not be published. Required fields are marked *