A QUICK INTRODUCTION TO SOCKETS
A socket is an endpoint for the communication between two machines over a network and it represents the base for client-server communication. Once a socket connection is established between these two, the client can access a "service" on the server machine. As an example, services can be an HTTP request (a web page) or an FTP request (file transer), etc...
Surfing the web is a nice example of this process: every time you connect to www.gotoandplay.it with your internet browser you establish a socket connection with a server machine and once this is done your browser can request a page to a specific service through a communication protocol called HTTP.
Services are also referred as "ports", and they represent distinct channels of communication that can be used by the client
and server to exchange data. Ports are described by a 16-bit integer value, which means you can virtually have a maximum of 65535 and among these the first 1024 are called "well known ports".
An example of these well-known-ports can be HTTP (80), FTP (21), POP (110), SMTP (25), TELNET (23), SSH (22), etc...
WHAT IS A SOCKET SERVER AND HOW IT WORKS
Multiplayer applications let you interact with other users in real-time thanks to a socket service specifically designed for the app/game functionalities. One of the simplest multiuser application I can think of is a chatting system: it mainly consists of a server-side socket service and a client application that sends and displays the user's messages.
Picture #1 explains this mechanism:
Picture 1
- the client connects to the server application;
- the user can receive messages from all other clients;
- every time the client sends a message, this is broadcasted on all the other clients connected.
THE CLIENT-SIDE: FLASH MX AND SOCKET COMMUNICATION
Flash MX has built-in support for socket connection with the XMLSocket object. This object lets you establish a communication channel with a socket server application and it is based around XML as a data-format for messages in both directions.
Creating an XMLSocket object in actionscript is very simple:
mySocket = new XMLSocket()
By inspecting the Flash MX reference you'll see that only 3 methods are available for this object:
- connect(host, port) = tries to connect to the specified host on the port number specified;
- send(xmlObj) = sends and XML Object to the server;
- close() = shuts down the connection.
Also four events are provided for the XMLSocket Object:
- onConnect = this is fired when the connection is established with the server;
- onData = when some data is received this handler automatically invokes the onXML event; this feature is discussed later in the article;
- onXML = invoked when and XML Object is sent to the client; the XML Object must be terminated with a 0 byte, otherwise the event won't fire;
- onClose = fired every time the connection is closed by the server.
By using the XMLSocket object and providing the functions that handle the object events you can start experimenting with the beautiful world of instant messaging and multiuser games/applications.
Before diving into a fully functional example, we need to see what happens on the other endpoint, the server.
THE SERVER-SIDE
Flash is a great tool for developing multiuser applications, but it must be paired with some other technologies to implement the server-side part of the system.
The choice here is not simple as there are really many different languages that could be employed to accomplish this task: C++, Java, C#, Python, Perl, Visual Basic, standalone PHP, and many more...
However one of the most common option is Java for a number of reasons: first of all it is very well known for its portability and this means that your Java application can be executed on a number of different operating systems without changing one single line of code!
Also Java has proved to be highly stable and its performance is really good, making it a good candidate for this job.
Here follows an example of a very simple java socket connector that acts as a sever:
import java.io.*;
import java.net.*;
public class simpleServer
{
public static void main(String args[])
{
// Message terminator
char EOF = (char)0x00;
try
{
// create a serverSocket connection on port 9999
ServerSocket s = new ServerSocket(9999);
System.out.println("Server started. Waiting for connections...");
// wait for incoming connections
Socket incoming = s.accept();
BufferedReader data_in = new BufferedReader(
new InputStreamReader(incoming.getInputStream()));
PrintWriter data_out = new PrintWriter(incoming.getOutputStream());
data_out.println("Welcome! type EXIT to quit." + EOF);
data_out.flush();
boolean quit = false;
// Waits for the EXIT command
while (!quit)
{
String msg = data_in.readLine();
if (msg == null) quit = true;
if (!msg.trim().equals("EXIT"))
{
data_out.println("You sayed: <b>"+msg.trim()+"</b>"+EOF);
data_out.flush();
}
else
{
quit = true;
}
}
}
catch (Exception e)
{
System.out.println("Connection lost");
}
}
}
If you have some basic Java knowledge it shouldn't be difficult to grasp the concepts in this example.
I'll try to make it even clearer for you:
- a new ServerSocket is instantiated and port 9999 is used for communication;
- using s.accept() the application stops and waits for an incoming connection;
- when a client connects, two objects are created for handling input and output on the Socket (data_in, data_out);
- a welcome message is sent to the client;
- a loop is established so that every message sent by the user is sent back to prove that the server is working;
- if the client sends the string "EXIT" the loop stops and the server quits.
Even if you're not familiar with the Java language and its API, it should be clear what is going on behind the scenes and you could try to implement it using a language you're more comfortable with.
(continues on page 2) |