Game Development Reference
In-Depth Information
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 im-
mediately.
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.
Search WWH ::




Custom Search