I haven’t seen many examples of connecting Adobe’s Flash to a Ruby socket server. I’ve seen several examples in Java, Python, Perl, etc. but not Ruby. Today I’m going to build a very simple threaded socket server that will work with Flash CS4 (it should also work with Flash CS3).
For this project I used Ruby 1.8.7 and Flash CS4 (ActionScript 3.0). I ran the socket server on Ubuntu 10.04 (Beta 1) inside of VMWare Player 7 hosted on Windows Vista.
Here is the Ruby code:
require 'socket'
socketServer = TCPServer.open(843)
puts "Starting the socket server"
while true
Thread.new(socketServer.accept) do |connection|
puts "Accepting connection from: #{connection.peeraddr[2]}"
begin
while connection
incomingData = connection.gets("\0")
if incomingData != nil
incomingData = incomingData.chomp
end
puts "Incoming: #{incomingData}"
if incomingData == "DISCONNECT\0"
puts "Received: DISCONNECT, closed connection"
connection.close
break
else
connection.puts "You said: #{incomingData}"
connection.flush
end
end
rescue Exception => e
puts "#{ e } (#{ e.class })"
ensure
connection.close
puts "ensure: Closing"
end
end
end
Here is the ActionScript code to connect to the socket server on port 843:
var xmlSocket:XMLSocket = new XMLSocket();
// Adjust this to the IP address of your Linux machine/vm
xmlSocket.connect("192.168.1.100", 843);
xmlSocket.addEventListener(DataEvent.DATA, onIncomingData);
send_btn.addEventListener(MouseEvent.CLICK, clickHandler);
disconnect_btn.addEventListener(MouseEvent.CLICK, disconnectHandler);
function clickHandler(event:MouseEvent):void
{
xmlSocket.send(input_txt.text);
input_txt.text = "";
}
function disconnectHandler(event:MouseEvent):void
{
xmlSocket.send("DISCONNECT");
// You could use this if you wanted to but I
// wanted the socket server the do the disconnect
//xmlSocket.close();
send_btn.enabled = false;
}
function onIncomingData(event:DataEvent):void
{
trace("[" + event.type + "] " + event.data);
output_txt.text += event.data + "\n";
output_txt.verticalScrollPosition = output_txt.maxVerticalScrollPosition;
}
I named the interface components as such:
textarea – output_txt
textinput – input_txt
button (send) – send_btn
button (disconnect) – disconnect_btn
Keep in mind if you run this outside of the Flash IDE environment you will need to implement code to serve the Flash security policy file.






It fastest than java server?
Can you explain, how to make Multi Threaded? Thanks!
@ManFromRussia,
This solution is not faster than a properly coded Java socket server.
Chad
Hi Chad,
Ive only just come across your blog and it seems to be something that I am trying to do.
I need to create an XML Socket where I can allow Flash to send back an XML file.
But I am a bit unsure on how to use your code to achieve this.
Would it be possible to help me out?
many thanks,
oz
@Ozzi,
If your looking to load an XML file then you would be better off using URLLoader and URLRequest. That way you can simply request the XML file and load it.
You can see the URLLoader in action in this article of mine:
http://www.giantflyingsaucer.com/blog/?p=1893
Parsing the incoming XML is pretty easy, this article can get you started on that aspect:
http://www.giantflyingsaucer.com/blog/?p=668
Chad