Menu

Client Manual

Janet Hunt

Client Service

Preparations

  • In order to run, a client must be created and connected. If you want to communicate with the client, you have to add a ConnectionListener as well. Also keep in mind that a connection can only be established to a JennyNet software layer of same protocol version on the remote end.
  • Between creation and connection of the client is the right time to set any specific parameters, in case that should be required. Otherwise the client draws its parameter settings from the global JennyNet layer (static class). What parameters are available can be seen in Parameter Settings.
  • It is also the right time to add a ConnectionListener to the client. This is essential because probably you will like to receive the replies from remote and these are broadcast only to the list of connection listeners.
  • A client is easily created. It suffices to call the empty constructor on Client. A client can be explicitly bound to a specific PORT address (ranging from 1 to 65535) of the local computer. If this doesn't happen, and normally it isn't required, a random free port is selected on the local system during connection. Binding can occur either on the constructor or through one of the bind() methods. Binding can take place only once per client instance.
  • The following are example initialisations of clients.

// create a client and bind it to port 4056
Client client = new Client();
client.bind(4056);

// alternate way of creation
Client client = new Client(4056);

// perform some initialisations
client.getParameters().setTransmissionParcelSize(64000);
client.getParameters().setObjectQueueCapacity(100);
client.getParameters().setFileRootDir("/home/xaver/jennychat/files");

// add a connection listener so you can get the replies and events
ConnectionListener listener = new OurConnectionListener();
client.addListener(listener);

Connecting a Client

Next step is to connect the client to a server via one of the connect() methods. This basically takes only two parameters: a timeout value in milliseconds and a server socket address (IP-address plus port number). The connect methods block until success or failure become evident or timeout has expired. There is always a timeout active during connect operations. If the given timeout value is zero or negative, a default value is derived from the CONFIRM_TIMEOUT parameter. There are 3 variants of the connect method dealing with different ways to pass a server socket address. After connection is established, the client is operational and can be referenced through the Connection interface.


int timeout = 30000;

// this calls domain name "jennyserver.com" at their port 2300
client.connect(timeout, "jennyserver.com", 2300);

// this draws an IP address and calls at their port 2300
InetAddress ipAddress = InetAddress.getByName("84.204.136.23");
client.connect(timeout, ipAddress, 2300);

// this calls domain and port bundled into one parameter
InetSocketAddress server = new InetSocketAddress("jennyserver.com", 2300);
client.connect(timeout, server);

Connecting may fail due to timeout, unavailability of the given server address or mismatch of services. In all cases exceptions are thrown. JennyNet offers a rather fine-grane facility to detect the causes via a set of exceptions. Details can be found in the IClient interface.

Communicating over the Connection Interface

Client is an extension of class ConnectionImpl implementing the IClient interface. In fact it is just a Connection with socket creation and connecting ability. JennyNet connections on client and server side share the same communication interface and potential. Details on how to use them are to be found on the Connections page.


Next


Related

Wiki: Connections Manual
Wiki: Manual Overview
Wiki: Parameter Settings
Wiki: Server Manual
Wiki: User Manual