What is a Socket?
A Socket is a mechanism used to send data over a network (e.g. the Internet), it is the combination of an IP address and a port. The IP address is used to specify the computer (on the network) to connect to and the port (which is not a physical port just a software construct represented by a number) specifies which port on the computer to connect to. There are multiple ports to allow multiple distinct connections to a single IP address, certain ports traditionally have certain uses for example HTTP servers use 80 and POP3 servers use 110.
Restrictions and Security
ActionScript has many restrictions that (to be brutally honest) cripple, but not entirely, the usefulness of Sockets.
Firstly, there are security issues, for a Flash application to connect to a particular server that server must host a policy file defining what ports Flash is allowed to connect to. This restricts Flash from connecting to most of the servers on the Internet although it isn’t too much trouble to put a policy file on your server.
Secondly, Flash isn’t allowed to create server sockets (sockets that clients can connect to) either, so to make a multiplayer game would require that the game is hosted on a server which all of the clients connect to. The server would have to constantly send data to all of the clients; this could potentially use a lot of bandwidth.
Finally, only one type of transportation protocol is supported that is TCP. Although TCP is very good for sending data like webpages and such it is not so good at sending real-time data. This means that multiplayer real-time games are slower than they could be or infeasible.
Never mind all that though, Flash Sockets are still useful.
Creating a Socket
Creating a socket is fantastically straight forward, since there are no decisions to make about the type of Socket. Here is an example with all of the event listeners added:
import flash.net.Socket;
import flash.events.*;
var socket:Socket = new Socket();
Security.allowDomain("*");
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
socket.connect("hejp.co.uk", 80);
It is wise to connect
after adding the event listeners because an event could be dispatched without any code to respond to it, or something strange like that. This example connects to hejp.co.uk using port 80 i.e. my web server.
Quick description of the Events:
- CONNECT: dispatched when the connection is established
- CLOSE: dispatched when the connection is closed
- IO_ERROR: dispatched when an error occurs
- SOCKET_DATA: dispatched when socket can be read
- SECURITY_ERROR: dispatched when security gets in the way
Using the Socket
Using the socket involves implementing the listener functions. In this example we’ll make a request for the index page of hejp.co.uk and trace the response.
Doing so involves writing a HTTP request to the port and then reading the response.
function onConnect(e:Event):void {
socket.writeUTFBytes("GET / HTTP/1.1\n");
socket.writeUTFBytes("Host: hejp.co.uk\n");
socket.writeUTFBytes("\n");
}
function onClose(e:Event):void {
// Security error is thrown if this line is excluded
socket.close();
}
function onError(e:IOErrorEvent):void {
trace("IO Error: "+e);
}
function onSecError(e:SecurityErrorEvent):void {
trace("Security Error: "+e);
}
function onResponse(e:ProgressEvent):void {
if (socket.bytesAvailable>0) {
trace(socket.readUTFBytes(socket.bytesAvailable));
}
}
Notes
A Flash app running the Flash IDE runs in a completely different security sandbox to a Flash app running in the browser, therefore even though an works running in the IDE errors (usually security errors) can crop up while running in the browser.
April 13th, 2010 at 7:56 pm
Hi,
I am making a multi-user flash application, and I am desperately in need of some help. I want to use socket programming, so that two people can work in real-time.
I want to transfer the mouse cordinates from one user to the other connected user, and from the other to the 1st.
what should i do??
please help.
thanks
p.s. I found the information very helpful.
[Reply]
hejp Reply:
April 16th, 2010 at 3:25 pm
Hello Rachit,
I’m afraid this can’t be done purely in Flash because of security issues the Flash Player can’t open a ServerSocket, which is necessary for the two computers to connect directly.
If you really need to use AS3 you can write a Adobe AIR desktop application, otherwise you could use Java for example which has very good libraries for network communication and GUI creation.
Alternatively you could write a PHP script (on a remote server) or Java application (remote or local) to act as a proxy, relaying the mouse co-ords.
Hope this made sense.
[Reply]
January 8th, 2011 at 7:12 am
can you help us with our project? socket programming in flash-created game. we know nothing about socket programming…even in adobe flash. but we already have our game at the moment..our problem now is with regards to socket programming.
regards,
kevin
[Reply]
January 20th, 2011 at 4:05 pm
thanks for this great tut, i have a problem in my socket connection i don’t know why it desconnect after sending a message by calling writeUTFBytes() i create a php socket script, the console displays my message and in my swf the data is recived, but after i recive dta it disconnect directly please help me am working on important project
thanks again
[Reply]
hejp Reply:
January 20th, 2011 at 5:43 pm
Hi Wajdi,
It’s tricky to tell since any one of many causes could be to blame.
Try and add an event listener to listen for EOFError events, and see if an exception is being caught and closing the socket.
Are you sure the php socket isn’t closing? Maybe you can use telnet to test the php.
[Reply]
July 8th, 2011 at 7:12 pm
How to load a different HTML document in another directory?
[Reply]
July 18th, 2011 at 8:16 am
Hi, thanks for for sharing that great post. I have a problem with the close socket event. When I disconnect my wifi, the event is not dispatched. Any clue?
[Reply]
March 8th, 2012 at 2:10 pm
Just to say thanks for this great article
It helped me a lot
[Reply]
October 8th, 2012 at 1:39 pm
Hi,
I am getting this error
IO Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2031: Socket Error. URL: 127.0.0.1"]
[Reply]
December 6th, 2012 at 4:26 pm
Very useful post, thank you! With that I’m trying to do this: http://stackoverflow.com/questions/13746553/send-file-from-c-sharp-server-to-as3-flash-clients
If by any chance you could give some input it would be great!
[Reply]