Game Development Reference
In-Depth Information
How it works...
The Network class is the main class used when setting up and connecting our compon-
ents. This particular method is the simplest way to create a server, simply stating a port to
listen to. Let's set different ports for TCP and UDP and supply the name and version of the
server.
The connectToServer method creates a client and connects it to the specified address
and port. Like in the server case, there are other convenient methods in Network that let
us specify more parameters if we want.
That's actually all that's needed. When running the two programs in parallel, we should see
the client connected to the server. There is no verification, however, that anything has
happened. That's why we added ConnectionListener at the end. It's an interface with
two methods: connectionAdded and connectionRemoved . These methods will be
called whenever a client connects or disconnects. These methods gave the server a way to
communicate to us that a connection has happened. These methods will be sources for a
chain of events in more advanced recipes.
Once the server is started, it begins to listen for incoming connections on the specified port.
If the network address is considered the street name, the port will be the door that will be
opened and made passable. So far, a mere handshake between the server and client has
been made at the doorstep.
Search WWH ::




Custom Search