Networking REALbasic
Volume Number: 16
Issue Number: 2
Column Tag: Emerging Technologies
Network with REALbasic
By Erick J. Tejkowski
Make your own network game in REALbasic
These days it seems everyone is jumping on the network bandwagon. From chat
applications to games, from CD databases to meta-search engines like Sherlock,
network functionality is an important addition to many types of software. Luckily,
REALbasic affords us the ability to quickly and easily add TCP/IP functionality to any
application. To explore this area of REALbasic programming, we will build a simple
network version of the ever-popular game Tic-Tac-Toe. After building the game, you
should know enough about the basic principles of socket applications that you will be
able to begin exploring network programming for yourself.
Besides an Address and Port, your application should also have a protocol. A protocol is
a small "language" that your application will use to communicate with other
computers. Many established protocols are already out there for you to use (http,
SMTP, et al), but for this example we will construct our own simple tic-tac-toe
protocol. Our protocol will use three commands: Opponent Moved, Opponent Quit, and I
Win. These are the three commands that our application will send and receive. To speed
things up, we will abbreviate the commands in a group of four letters followed by a
number in two cases. Listing 1 details the commands in the protocol.
The heart of REALbasic TCP programming is the Socket control (see Figure 1). It is
through the Socket control which your application will "talk" to the outside world. As
such, a Socket control has two important properties. They include Address and Port.
Figure 1. The Socket Control.
The Address property is the IP address, which can be found in the TCP control panel or
the Remote Access log if you are using dialup networking. The property is a string and
can be set directly within the REALbasic IDE or later by code in your program during
runtime. The Port property is an integer that tells a socket control where to focus its
sending and receiving efforts. A TCP port is somewhat analogous to a television
channel. Just as particular types of content appear on certain television channels,
certain types of information appear on specific port numbers. Some port numbers are
already reserved for use by protocols covering web, email, chat, telnet
communication, and many more, while other port numbers are free for you to use as
you see fit. A nice list of reserved port numbers can be found in the reference section
of this article. For example, port 80 is used for http communication (i.e. web pages),
while port 25 is used for Simple Mail Transfer. For our example, we will use port
815.
Listing 1. - A Simple Tic-Tac-Toe Protocol
Command Function [TOKEN:20577]rameters
oppm# Opponent Moved Requires an integer indicating location of
move
opqu Opponent Quit [TOKEN:20079]ne
iwin# I Win Requires an integer indicating location of win
How this protocol is used will become more apparent as the project progresses, so be
patient if it doesn't make sense to you yet.
One final point to note about our project is that it will really be built in two parts. One
game board will be the server, while the other will act as the client. Although this
functionality could be built into a single application, it will help you to see how a
client differs from a server by separating the functionality. Despite this separation,
however, you will quickly notice that the two applications share a lot of the same code.
Both will respond to and send move "events". The difference lies in the initialization of