Archive for Web Services
Simple Flex Socket Client
Posted by: | CommentsClient-server programming is one of my passions, and I enjoy doing it in almost any language. Which makes it fitting that Flex (ActionScript) is one of my favorite languages to develop in, because it truly is a client-side technology which pretty much means it’s open to just about any type of backend server. I suppose you could make that argument with just about any language depending on how it’s being used, but since Flex isn’t processed and returned to the browser as HTML, that makes it a little different in my opinion.
Even in Flex, most applications will connect to a backend server that’s running an HTTP service (which queries the database on behalf of Flex), but I’m even going to depart from that in this post. I’m going to be demonstrating how to make socket calls to another service. You could apply this to making HTTP requests on port 80, make requests directly to a MySQL server running on port 3306. In this particular post, I will keep it simple by demonstrating how to make an HTTP request.
Declaring and initializing the Socket
You declare socket like this:
import flash.net.Socket; private var socket : Socket;
Inside an initialization method, we will add a listener for the CONNECT event, and the SOCKET_DATA event. The CONNECT event alerts us when the connection has been made, and allows us to send our request. The SOCKET_DATA event is triggered whenever there is data waiting to be read from the socket.
socket = new Socket(); socket.addEventListener( Event.CONNECT, onConnect); socker.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData);
Connecting and Sending Data
At some point you will need to call connect on the socket, which accepts the URL of the server and the port it will be connecting to. In the onConnect() method, you will write the request using one of the writeXXX() methods, and call flush() which actually sends the data over the wire.
socket.connect(txtUrl.text, 80);
private function onConnect(event : Event) : void {
var requestString : String = getHttp_1_1Request();
socket.writeUTFBytes(requestString);
socket.flush();
}
Reading Data
As not all socket data will be available at once, as in most client programs, you will want to read the data in a loop, using one of the readXXX() methods.
private function onSocketData(event : ProgressEvent) : void {
while (socket.bytesAvailable) {
txtResponse.text += socket.readUTFBytes(socket.bytesAvailable);
}
socket.close();
}
In general you would be checking for the end of the response you’re expecting (based on protocol), to simplify things I simply close the socket after the while loop exits the first time. If you leave the socket open for too long without reading data, I have noticed that I receive the following error, regardless of whether I have the correct policy file including cross-domain.xml:
Error #2044: Unhandled securityError:. filename.swf text=Error #2048: Security sandbox violation cannot load data from the.host.name
In this example, the raw output received from the socket request will be shown in the text box. You can download the complete source to this example here (where you may also view a screencast demo of this procedure).
Depending on what version of the Flash Player you’re running, this example may not run as-is if the server you are connecting to does not implement the policy-file (served from port 843. I will be discussing this in an upcoming post where I delve into a little more advanced socket programming in Flex, including how and where to deploy a master policy file.
Book Review: Programming Amazon Web Services
Posted by: | CommentsI don’t know if they’re just a more established tech book publishing company, but I usually have a good experience with O’Reilly books. Programming Amazon Web Services, subtitled S3, EC2, SQS, FPS, and SimpleDB, by James Murty, was great. 5 stars.
I enjoyed this book mainly because I love using Amazon’s web services for recreation and work. If I didn’t enjoy Amazon’s web services in the first place I probably would have found the book excessively detailed. In chapter 5 the author writes, “This chapter delves into the nitty-gritty aspects of running a Linux server in EC2,” and he ain’t kidding! This book really gets down into the API (and this is true for all the services treated in the book, not just EC2).
So if you’re looking to do some casual computing on EC2 or S3, you’d probably be better off without this book. I’d recommend installing the Firefox plugins for EC2 and S3, and going from there. Here’s a link (from the web site of a class I took last fall) that will probably be useful to someone in that situation. On that page you’ll find links to some tutorial pages, and a webcast or two.
On the other hand, if your intention is one of the following:
- Author a tool similar to the Firefox EC2 plugin.
- Create complex scripts to manage your EC2 instances or S3 buckets.
- Write a code library for any of the Amazon web services.
- Increase your understanding of what’s going on when you use the Firefox plugins.
Then this is the book for you.
That said, this book exposed me to FPS and SimpleDB for the first time (never had a chance to use either). As far as EC2, S3, and SQS go… I didn’t really learn how to do anything new with them from this book, per se. But it did significantly increase the depth of my understanding regarding each of these services. There’s a benefit to depth of knowledge with these kinds of technologies, because I’m sure I’ll encounter a problem in the future that can be solved with these tools, whose solution I might have overlooked before.
Lab 5 : PHP and SOAP
Posted by: | CommentsThe only SOAP requests I’ve ever made were made on the .NET platform. They’re not that much of a beast on .NET, but it wasn’t exactly a cake walk either. So I had been bracing myself for the worst trying to implement it in PHP.
I should explain that my lab 5 client connects to a PHP service that in turn makes the SQS requests, etc. I initially wanted to implement an SQS library in Actionscript (and probably will in the future when I’m not pressed by deadlines) but I decided it was too ambitious for the amount of time I wanted to spend on this lab. So alas, a PHP service also handles my SOAP request to WHOIS.
Anyway, I was expecting SOAP on PHP to be a seriously complex affair. Here’s my code that makes the request:
$client = new SOAPClient("http://www.webservicex.net/whois.asmx?WSDL");
$params = array('HostName' => $_GET['url']);
$whois = $client->GetWhoIS($params);
Granted, it would have required about 2 more lines if there wasn’t a URL to the WSDL, but it doesn’t get much simpler than that. I should mention that this requires that PHP SOAP be enabled (uncomment a line in your php.ini if you’re running Windows; recompile from source using ‘enable-soap’ if you’re running Linux). I didn’t have to recompile, thanks once again to Remi Collet (the French guy who has yum rpms for all this stuff, see my previous post).
Well, the SQS library I’m using is pretty old and doesn’t have a means of querying the queue for the number of messages. So, I thought I’d try sending a SOAP message to Amazon to get it. Amazon’s WSDL is a little more complex, and I probably could have gotten it to work if I wanted to play around with the messages for another hour or so. It turned out to be a miserable failure, and I resorted to my old tricks: (file_get_contents()) which worked perfectly. Here’s the code I used, which shows the query string needed to get the number of messages:
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$qs = "http://queue.amazonaws.com/A3N3IV5XJH079S/processing" .
"?Action=GetQueueAttributes" .
"&Attribute=ApproximateNumberOfMessages" .
"&AWSAccessKeyId=[AMAZON_ACCESS_KEY]" .
"&Version=2007-05-01" .
"&Timestamp=" . urlencode($timestamp) .
"&Signature=" . urlencode(constructSig('GetQueueAttributes' . $timestamp));
$response = file_get_contents($qs);
The constructSig is the same method I listed in a previous post.
Here are a few links that were helpful:
SQS Query and SOAP API
Getting SQS Attributes
SQS WSDL

