Page 1 of 1

Flash/Python socket connections and communication Rate Topic: -----

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Posted 04 October 2008 - 09:42 PM

Before we start
This tutorial requires that you have Flash and Python installed on your computer. To download Python go here:
http://python.org/

In this tutroail
We will be creating a simple Flash client side script that will open a socket connection with our server (which we will be creating in Python), recieve a message from the server and send a message to the server, which the server will acknowledge and print out as well.

NOTE - This tutorial uses the Flash Sandbox, and doesn't work for general web activity at this point in time.

Creating the server
The server script for this tutorial is a simple script that will begin listening for socket connections on port number 2727. When a socket connection is made the server will print the client information (IP and socket), print out the data the client sent, and then send a reply to the client.

Starting out we will need to import the sockets class, otherwise the script is dead in the water:
import socket


Now, we need to get the server to allow socket connections, bind to a port, and start listneing for connection attempts.

This can be done in 3 lines:
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2727 ) )
mySocket.listen ( 5 )

Basically all that happens is the server creates a variable called myScoket amd sets it to the socket input stream. Then it binds mySocket to no host (defaults to localhost) and port number 2727. After that it starts listening for socket connections and will allow a total of 5 connections to be open at once.

Now that the hard part is done we need to add a handle for incoming socket connections and messages:
while True:
	channel, details = mySocket.accept()
	print 'We have opened a connection with', details
	txt = channel.recv ( 100 )
	print txt
	channel.send ( "This is a response test - attempt 1\0" )
	channel.close()

This creates an infinite loop (to keep the application running until you close it) and allow connections. Upon receiving a connection the variables channel and details are set. The details are IP address and socket that the client is communicating from, and the channel is the data stream.

Once the socket connection has been accepted the server outputs We have opened a connection with <IP, PORT> Then recieves the data which was sent (100 bits at a time until the data stream is finished), and prints the message out. After completing this the server sends a reply to the client in plain text.

NOTICE - The reply message ends with \0 this is a null byte character, which Flash requires to know that the end of the data stream has come.

And, after the reply is sent the server halts the connection.

Here is a look at the whole thing:
import socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2727 ) )
mySocket.listen ( 5 )
while True:
	channel, details = mySocket.accept()
	print 'We have opened a connection with', details
	txt = channel.recv ( 100 )
	print txt
	channel.send ( "This is a response test - attempt 1\0" )
	channel.close()


The Flash side
On the Flash side of life things are a bit easier, all we have to do is create a new XMLSocket, tell it what port and host to connect with and give it a few functions on how to act under certain circumstances.

Starting off, lets create the socket variable:
var sock:XMLSocket = new XMLSocket();

This, as you would expect, creates a new XMLSocket called sock.

Connecting with the server is as easy as this:
sock.connect('localhost', 2727);

Which tells the socket to go for the localhost on port 2727 - The same port the server is listening on.


New, we need to set up a few functions:
sock.onConnect = function(myStatus:Boolean){
	if(myStatus){
		trace("Connected");
	}
	else{
		trace("Connection failed");
	}
}
sock.onData = function(msg:String){
	  trace("Recieved: "+msg);
}
sock.onClose = function(){
	trace("Connection Closed");
}


The above functions tell the socket connection how to act given the various calls allowed by ActionScript 2.0, and output the various messages accordingly. By this time (after reading a few of my other tutorials) I hope you will be able to understand what is going on there.

Now, the simple thing is left - sending a message to the server. This is accomplished like so:
sock.send("testing it out - Flash style :)");


The full AS 2.0 code:
var sock:XMLSocket = new XMLSocket();
sock.connect('localhost', 2727);
sock.onConnect = function(myStatus:Boolean){
	if(myStatus){
		trace("Connected");
	}
	else{
		trace("Connection failed");
	}
}
sock.onData = function(msg:String){
	  trace("Recieved: "+msg);
}
sock.onClose = function(){
	trace("Connection Closed");
}
sock.send("testing it out - Flash style :)");


Running it
When you have everything finished, you will want to run it. This is accomplished simply by starting up the server (this must be done first), and then in your .fla file (In Flash) pressing CTRL+Enter (this will fire up the Flash SWF in the Flash sandbox and allows for a TON of security pass overs. As this is the case you will be albe to see the script working without having to jump through any hoops.

Here is what you will get as output:
Server:

Quote

We have opened a connection with <'127.0.0.1', 1364>
testing it out - Flash style :)


Flash

Quote

Connected
Recieved: This is a response test - attempt 1
Connection Closed


The End
Hopefully you enjoyed this tutorial and can see some useful applications of it. I will probably post another tutorial when I get things working online so you can have things playing outside of the sandbox.

Until next time - Keep Flashing :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1