Reputation: 1399
I have a C program and that runs a web server. I have a Air Application and I want to communicate with that web server using Air Application. I create a socket object and do the following.
public function httpTest():void
{
sock.addEventListener(Event.CONNECT, onConnect);
sock.addEventListener(ProgressEvent.SOCKET_DATA, onDataRecv);
sock.addEventListener(IOErrorEvent.IO_ERROR, onError);
try
{
trace("Connecting...");
sock.connect("127.0.0.1", 9800);
sock.writeMultiByte("GET /Connection?data=version", "us-ascii");
sock.flush();
}
catch(err:Error)
{
trace(err.message);
}
}
public function onConnect(event:Event):void
{
trace("onConnect +");
}
public function onDataRecv(event:ProgressEvent):void
{
trace("onDataRecv +");
}
public function onError(event:Event):void
{
trace("onError +");
}
socket connects successfully and its connection event is fired. but when I try to request the connection url nothing is received on server side. am I missing something. Thanks
Upvotes: 0
Views: 80
Reputation: 1399
I made a mistake. I did not add HTTP version and string terminator in the Get String. Thats why I did not receive any print on server side. Because the request was invalid. I posted the working code. Thanks for help.
public function httpTest():void
{
sock.addEventListener(Event.CONNECT, onConnect);
sock.addEventListener(ProgressEvent.SOCKET_DATA, onDataRecv);
sock.addEventListener(IOErrorEvent.IO_ERROR, onError);
try
{
trace("Connecting...");
sock.connect("127.0.0.1", 9800);
sock.writeMultiByte("GET /Connection?data=version HTTP/1.0\r\n\r\n", "us-ascii");
sock.flush();
}
catch(err:Error)
{
trace(err.message);
}
}
public function onConnect(event:Event):void
{
trace("onConnect +");
}
public function onDataRecv(event:ProgressEvent):void
{
trace("onDataRecv +");
}
public function onError(event:Event):void
{
trace("onError +");
}
Upvotes: 0
Reputation: 1111
Like just about everything to do with networking in Flex, socket.connect
is asynchronous and non-blocking, meaning that just because sock.connect
has returned without error it doesn't mean the socket is actually ready for use yet. I suspect that if you put trace(sock.connected)
in your original code after your call to writeMultiByte
it will print false
.
You will need to delay your sock.writeMultiByte
call until the connection is ready, which isn't until your onConnect
handler fires. Try:
try
{
trace("Connecting...");
sock.connect("127.0.0.1", 9800);
}
catch(err:Error)
{
trace(err.message);
}
...
public function onConnect(event:Event):void
{
trace("onConnect +");
sock.writeMultiByte("GET /Connection?data=version", "us-ascii");
sock.flush();
}
Upvotes: 2