Skip to content

Client Manual

Wolfgang Keller edited this page Jan 29, 2025 · 4 revisions

Client Service

A client (class Client) is a single connection (interface Connection) seeking to connect with a server. It has a local and a remote socket address, each consisting of a domain-name and a port-number.

Preparations

  • In order to run, a Client must be created and connected. A connection can only be established to a JennyNet software layer of the same protocol version on the remote host. For communicating with the client, the application adds a ConnectionListener for receiving events and addresses the client's Connection interface for sending.
  • As an alternative to listening for events (which is the intrusion of an alien thread), the application can poll the client for events. For this the client has to be wrapped into a ConnectionPollService instance.
  • Between creation and connection of the client is the right time to set specific connection-parameters, if required. By default the client draws its parameter settings from the global (static) JennyNet class. With setting parameters, features and services of a connection can be tailored or activated. What parameters and services are available can be seen in Parameter Settings.
  • For instantiating a Client, it suffices to call the empty constructor. 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 during connection. Binding can occur either on the constructor or through one of the bind() methods. Binding can take place only once per instance.
  • Examples

// 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);

// wrap client into a poll-service
ConnectionPollService poll = new ConnectionPollService(client);

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. The connect methods block until success or failure becomes evident or timeout occurs. 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 of the connection. After connection is established, the client is operational.


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 these 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 implementing the Connection and the IClient interfaces. 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 can be found in the Connections page.


Home | Prev | Next

Clone this wiki locally