Very strange problem with UDP socket on Mac
Hi,
I'm trying in vain to make PHP and an AIR app communicate using UDP Socket (in multicast using 225.0.0.0). My PHP script must be sending some data to my AIR app. I choose the port 60000 for this. I also wrote a tiny C program that listen to the same port (I know that 2 programs can't bind to the same port simultaneously). The 3 programs are one the same machine, Mac OS X 10.6.7, Flash Builder 4.5, Air 2.6 and XCode 4.0.
**** Scenario #0 : (what I originally wanted to do)
- I launch the AIR app, listening to the port 60000
- I launch my PHP script sending some data
=> nothing is received, the DatagramSocketDataEvent.DATA is never called
**** Scenario #1
- I launch the AIR app, listening to the port 60000
- I launch my C program (XCode) listening to the SAME port, without any complain !!!
- I launch my PHP script, and it works, the AIR app receives what was send (and the C program too).
**** Scenario #2
- I launch the AIR app, listening to the port 60000
- I launch my C program (XCode) listening to another port.
- I launch my PHP script, and it works, the AIR app receives what was send.
*** Scenario #3
- I launch my C program listening to the port 60000
- I launch my AIR app, it complains that the port 60000 is already in use !
I don't have any explanation. Do you have some ?
My Flex Code :
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.events.DatagramSocketDataEvent;
import flash.net.DatagramSocket;
import mx.controls.Alert;
private var socket:DatagramSocket;
public function creationCompleteHandler(event:MouseEvent):void {
socket = new DatagramSocket();
socket.addEventListener(DatagramSocketDataEvent.DATA,dataReceived);
socket.addEventListener(IOErrorEvent.IO_ERROR,IOErrorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
socket.bind(60000,"225.0.0.0");
socket.receive();
Alert.show(socket.localAddress.toString()+' '+socket.localPort.toString());
}
public function IOErrorHandler(event:IOErrorEvent):void {
Alert.show("IOError");
}
public function securityErrorHandler(event:SecurityErrorEvent):void {
Alert.show("Security Error");
}
public function dataReceived(event:DatagramSocketDataEvent):void {
data.text = event.toString();
}
public function Envoyer(event:MouseEvent):void {
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://127.0.0.1/~william/panoramatch/data.php");
var variable:URLVariables = new URLVariables();
variable.commande = "scoreA";
variable.valeur = score.text;
request.method=URLRequestMethod.POST;
request.data = variable;
loader.load(request);
}
public function envoyerUDP(event:MouseEvent):void {
var data:ByteArray = new ByteArray;
data.writeUTFBytes(score.text);
socket.send(data,0,data.length);
}
]]>
</fx:Script>
<s:Button x="10" y="40" label="Envoyer" click="Envoyer(event);"/>
<s:TextInput x="10" y="10" id="score"/>
<s:TextArea x="10" y="69" width="456" editable="false" id="data"/>
<s:Button x="88" y="40" label="Bind" click="creationCompleteHandler(event)"/>
<s:Button x="166" y="40" label="Envoyer UDP" click="envoyerUDP(event)"/>
</s:WindowedApplication>
