Hardware Reference
In-Depth Information
Networked Pong
Networked games are a great way to learn about real-time connections. This project is a
networked variation on Pong. In honor of everyone's favorite network status command,
let's call it ping pong. The server will be a Processing program, and the clients will be
physical interfaces that connect through Ethernet-enabled Arduinos. The clients and
the server's screen have to be physically close so that everyone can see the screen. In
this case, you're using a network for its flexibility in handling multiple connections, not
for its ability to connect remote places.
A Test Chat Server
You need a server to get started. There's a lot of code to
control the pong display that you don't need right now
(you just want to confirm that the clients can connect), so
the following is a simple server with all the basic elements
to handle network communications. It will let you listen
for new clients, and then send them messages by typing
in the applet window that appears when you run the
program. Run the server and open a telnet connection to
it. Remember, it's listening on port 8080, so if your com-
puter's IP address is, say, 192.168.1.45, you'd connect like
so: telnet 192.168.1.45 8080 . If you're telnetting in from
the same machine, you can use: telnet localhost 8080 or
telnet 127.0.0.1 8080 .
From the Monski Pong project in Chapter 2, you're already
aware of the methods needed to move the paddles and
the ball, so some of the code will be familiar to you. As this
is a more complex variation, it's important to start with
a good description of the whole system. The system will
work like this:
• The game has two teams of multiple players.
• Each player can move a paddle back and forth. The
paddles are at the top and bottom of the screen, and
the ball moves from top to bottom.
• Players connect to the game server through a TCP
connection. Every time a player connects, another
paddle is added to the screen. New connections
alternate between the top and bottom teams. When
a player connects, the server replies with the following
string: hi , followed by a carriage return and a line feed
(shown as \r\n ).
• The client can send the following commands:
l (ASCII value 108): move left
r (ASCII value 114): move right
x (ASCII value 120): disconnect
• When the client sends x , the server replies with the
following string, and then ends the socket connection:
Whatever you type in the telnet window will show up in
the server's debugger pane, and whatever you type in
the server's applet window will show up at the client's
command line. However, you'll have to press Return after
each character in order for the server to see it—unless you
make a change after you connect.
On Mac OS X or Linux, press the telnet escape key combi-
nation (Ctrl-]), type the following, and then press Return:
bye\r\n
mode character
That's the communications protocol for the whole game.
Keep in mind that it doesn't define anything about the
physical form of the client object. As long as the client can
make a TCP connection to the server and can send and
receive the appropriate ASCII messages, it can work with
the server. You can attach any type of physical inputs to
the client, or you can write a client that sends all these
messages automatically, with no physical input from the
world at all (though that would be boring). Later in this
chapter, you'll see a few different clients, each of which
can connect to the server and play the game.
On Windows, telnet should not require any special con-
figuration, but if you find otherwise, press Ctrl-], type the
following, and press Return twice:
set mode stream
Now every character you type will be sent to the server as
soon as you type it.
X
Search WWH ::




Custom Search