Setting up a server and client

In this recipe, we'll look at the absolute minimum in order to get a server and client up and running and be able to talk to each other.

This is accomplished in just a few lines of code.

The server and client will share some common data that we'll store inside a properties file for easy access and external modification. First and foremost, the client must know the address of the server, and both server and client need to know which port to listen on and connect to. These would most likely be editable from within a game.

How to do it...

Perform the following steps to set up a server and client:

  1. In the constructor of the server class, we start by loading the properties file. Once done, we can initialize the server with the following lines of code:
    server = Network.createServer(Integer.parseInt(prop.getProperty("server.port")));
    server.start();

    In the static block, we must also make sure that the server doesn't shut down immediately.

  2. The client is set up in a similar way, shown as follows:
    client = Network.connectToServer(prop.getProperty("server.address"), Integer.parseInt(prop.getProperty("server.port")));
    client.start();
  3. To verify that a connection has taken place, we can add ConnectionListener to the server, as follows:
    public void connectionAdded(Server server, HostedConnection conn) {
      System.out.println("Player connected: " + conn.getAddress());
    }
  4. If we connect to the server again, we should see the message printed in the server's output window.

How it works...

The Network class is the main class used when setting up and connecting our components. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141