What Is a Socket Server?


Before we can get into the details of installing and using a socket server, it's important to understand what a socket server is. Put simply, a socket server also called a multiuser server is a server that listens for inbound client connections on a port and allows those clients to communicate with each other over a common protocol (which in the case of ElectroServer will be XML). That's quite a mouthful! Let's break this concept down into more digestible parts. We'll start with a review of the Internet basics on which a socket server depends.

Internet Basics

While I'm sure everyone reading this uses the Internet just about all the time, it's surprising to learn how little most developers know about how it actually works. I've always felt that the more knowledge you have about something, the more effective you can be in using it. In the case of socket servers, this knowledge can help you avoid the sticky deployment and security issues that so many people run into.

IP Address

While the Internet Protocol (IP) address isn't even close to being the lowest level of networking on the Internet, it's the lowest layer we need to worry about. An IP address means just what it says: It's essentially the address of a computer on the Internet. While there are exceptions, it's easiest to assume that each computer on the Internet has a unique IP address. An IP address, coupled with some other Internet technologies, is enough to let any machine find and communicate with any other machine over the Internet.

An IP address typically takes the form

XX.XX.XX.XX

where each segment can be a one, two, or three-digit number. For example, one of the Web servers for Macromedia.com is

65.57.83.12

These numbers are called dotted quads or just IPs. A single number in the IP is called an octet because its maximum size is 28, defined as a range of 0 255. At first glance, it may seem as if the number of IP addresses available is huge and we will never run out; but in reality we are already getting uncomfortably close to the limit. The current IPv4 specification allows for about 4.3 billion addresses, but with the explosive growth of users on the Internet, more and more IPs are needed every day.

graphics/afig01.gif

To this end, a new version of the IP specification called IPv6 is in the works. This specification adds improved multimedia streaming and better performance. But most important, it increases the IP address size from 32-bit to 128-bit.

How do you get an IP? In the case of home users, you are assigned an IP address when your machine connects to your ISP. If you are on dial-up, this means that every time you connect to the Internet, you typically get a new IP address. For the most part, cable and DSL modems work the same way, but as you are always connected, your IP usually won't change unless you reboot your machine. There are some exceptions to this, however. Many DSL providers are using a technology named PPPoE (Point-to-Point Protocol over Ethernet), which allows you to disconnect and reconnect with your machine still running. When this happens, you could be assigned a new IP address. Also, it is possible to "release" your current IP address and get another one, but the method for doing so depends on your operating system and there is no guarantee that it will be a new IP address.

graphics/afig02.gif

The illustration at left shows the typical home computer setup with a DSL or cable modem. You can also have something called a static IP address. This means that your IP doesn't change from one day to the next but stays the same even after your connection goes down or you have to reboot. Servers on the Internet almost always have static IP addresses, and there are few cases where you wouldn't want one.

So do you have to be online to get an IP address? Not at all! Any machine that supports TCP/IP (the protocol the Internet uses) always has an IP available to it. This is a reserved IP called the loopback IP, and you can connect to it using the IP 127.0.0.1 or the host name localhost. This IP is used for diagnostics and testing but can also be used by us Flash developers to run socket servers locally without anyone else's being able to access them. We will talk more about this later.

I said previously that each computer on the Internet has a unique IP address, and also that this is not strictly true. You've probably heard of firewalls and routers. These devices can be used to hide IP addresses from the Internet in various ways. Now before any of you groan and skip ahead, know that I bring this up because it directly relates to running a socket server on your home machine and getting people to see it on the Internet. Kind of important!

Here's a real-life example to illustrate the situation. If a small corporation wanted to provide its 20 employees with Internet access from their workstations through the company's dedicated ISDN line, how would it do this? One possible way would be to contact the company's ISP and have 20 IP addresses made available to its network. This constitutes a maintenance headache and a lot of administrative overhead. A simpler way would be to use a technology called Network Address Translation (NAT). NAT allows one computer to act as a gateway to the Internet for other computers. This computer would have one connection to the Internet with an externally available IP address, and another connection to the internal network with an internal address. This is usually accomplished by having two network cards. By "internal address," I mean an IP that only the internal network will understand. Often these start with 10 in the first octet. Each computer on the internal network would also have its own unique internal IP and would understand that to connect to the Internet, it needs to go through the gateway.

graphics/afig03.gif

I bring this up because many people with cable modems or DSL access use small router/firewall appliances made by companies such as Linksys, 3Com, or Intel. When you use one of these appliances, you cannot run a socket server on the internally designated IP address and expect people on the external Internet to see it without some configuration changes. Specifically, you can start the server without any problems, but when you give out your IP and port, people will not be able to connect to it. If this happens, you may need to use the DMZ, port, or IP forwarding feature of your appliance to allow external clients to connect to your server. A DMZ allows your computer to be in an unsecure location on your network, and thus the firewall allows anyone in. Port and IP forwarding are ways to tell your firewall to send all IP requests on a given port to your server rather than blocking them. One other solution to this problem is to open up that port on your firewall so that it is not restricted. Implementing any of these solutions is beyond the scope of this appendix. Look in your router/firewall appliance's documentation to see which will work best for you.

Ports

Thus far, we have talked about a computer's address on the Internet. This is a valid analogy on the surface, but it still needs some refinement. The typical computer on the Internet has one IP address but is often running many different Internet-related applications at the same time. As you know, Web browsers, instant messengers, email clients, multiplayer games, and other applications can all run simultaneously.

With only one address, how does your computer know which application needs the data you are receiving? This is where ports come into play. For every IP address on a computer there are 65,536 ports associated with it. If you think of every IP address as the real-world address of an apartment building, a port would be an apartment number.

This means that, theoretically, your desktop computer could be talking to 65,000+ remote computers at the same time without getting confused. (Realistically, a desktop machine couldn't handle that, but some servers are more than able to.) So when talking to a remote computer, you use not just the IP address but also a specific port to uniquely identify both the computer and the service/application with which you wish to communicate.

You would think that with 65,536 ports available, there would be enough for every service imaginable. And for the most part, there are but still, you have to be careful. Many ports are reserved for a specific functionality, such as HTTP, FTP, POP, SMTP, and many others. The general rule of thumb is that you should never use any port below 1024 for your own applications. In the case of Flash with XMLSocket, this is a hard rule Flash is unable to use a port below 1024. Below is a table of common ports used by various services.

Table 1. Commonly Used Ports
Internet Service TCP Port
HTTP 80
HTTPS 443
SMTP 25
POP3 110
FTP 21
Telnet 23
NMTP 119

Sockets

A socket is simply one endpoint of a two-way connection over a network. Any socket is defined by an IP and a port. When a server application is running, it "listens" on a socket for inbound connections from a client. A client, such as a Web browser, knows the IP or host name and port number of the server, and attempts to connect. If all goes properly, the server accepts the connection and then moves the client's connection to a different socket, which it picks internally. This is so that the server can continue listening for clients on the original socket, in order to accommodate more than one client at a time. Once they are connected through the internally picked socket, both the client and the server are able to communicate with each other directly.

Let's follow through with our example of a Web browser. You open your favorite Internet browser and type in http://www.macromedia.com. Through the wonder of DNS (that's a Domain Name Server, which maps an IP address to a domain name), that domain name gets translated to 65.57.83.12. The browser knows you are using HTTP; therefore it knows the port is 80. This is enough information to attempt to establish a socket connection to 65.57.83.12:80. Internally, the Web browser does exactly that. The Macromedia Web server accepts the connection and shuffles your browser to a different socket on its side. At this point, your browser is able to request a Web page. Once you have finished getting the page you want, your browser automatically closes the connection to the server and is able to move on. This is only a very simple description of what happens, but it gives you the basic idea.

Where Does Flash Fit In?

We have now talked about the basic underlying technologies used to establish TCP connections between computers on the Internet, and how a common application handles this transaction. Now, how does Flash accomplish this? Easy exactly the same way!

Flash does not handle sockets any differently than your Web browser does. The only difference is the protocol that's used. In a Web browser it's HTTP. When you receive email, it's POP3 or IMAP. For sending email, it's SMTP. In Flash it's XML, which you define yourself to best customize it to your task. (And Flash hasn't chosen a specific port number, so you can pick whatever port you want, as long as it's above 1024.)

Now that you have all that background material under your belt, we will talk specifically about socket servers and how they work with Flash.

Socket Servers

A socket server is just a server that listens on a socket for a connection from a Flash client, and accepts those connections when it hears them. That seems straightforward enough, but now you are probably thinking: What makes a socket server different from a Web server? Well, two things specifically: A socket server uses a different protocol, and its connections are state-full.

A state-full connection is one that remembers the user between interactions. This critical difference dramatically extends Flash's abilities over an application based on the HTTP protocol. The HTTP protocol is not state-full. There are many clever methods to make it appear to remember a client between requests, but in reality a Web page doesn't know if you visited it five minutes or five days ago. It is always up to the non-state-full application (any Web application using HTTP) to determine who you are, as well as to store any data it needs to remember about you. Conversely, the connections to a socket server are held open until you explicitly close them with code, or until you close the Flash application. This means that many transactions can occur over one connection, and that the server can remember you between transactions with little difficulty. This is necessary for chat functionality such as game history, accurate counts of people in a room, and so on.

The protocol for a Web server is HTTP. The high-level protocol for a socket server, as we've said, is XML. This is where the power and flexibility of a socket server come into play. By using XML as the high-level protocol, you are able to define your own specific (that is, user-defined) protocol for the application at hand. The multiuser game chapters in Part 3 will explain this in some detail.



Macromedia Flash MX Game Design Demystified(c) The Official Guide to Creating Games with Flash
Macromedia Flash MX Game Design Demystified: The Official Guide to Creating Games with Flash -- First 1st Printing -- CD Included
ISBN: B003HP4RW2
EAN: N/A
Year: 2005
Pages: 163
Authors: Jobe Makar

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net