Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Java ME Guideline: Network - Multiple Network Connections

 
 
[Design Guidelines - General Introduction] [Design Guidelines - Technical Overview]  

Contents
  1. Problem Description
  2. Solution Description
  3. List of Affected Devices
  4. Keywords
1. Problem Description

Some device do not allow opening multiple network connections.

Use Cases

Many connected applications may need multiple connections: browser, email, IM, Network games, Mapping/Location based applications).

2. Solution Description

Application design should avoid relying on multiple connections, this feature is fragmented. However, some application may take advantage of multiple connections, when available, to improve performance or user experience.

Solution Requirements

This solution is compatible with MIDP 1.0 and up

Solution Approach

Code the maximum (or lower) number of network connections in the JAD file, but not in the manifest, in order to avoid useless creating versions.

Solutions Overview

Create a tester midlet. This midlet will count the maximum number of allowed connections on a target device. The test midlet attempt to create several connections until it reaches the maximum number. When this maximum number of allowed connections is known, include it in JAD file attribute, but not in the JAR file manifest.

This number will be retrieved at runtime. The application shall maintain a global variable (application wide) with the current number of open connections.

Alternative Solutions

Hard coding the maximum number of allowed connections in the source code should be avoided, it introduces useless fragmentation.

Defensive programming, i.e. runtime detection of the maximum number of allowed connections should be avoided as it may have some impact on the user bill, and may create hard to understand security prompts.

Examples, Illustrations

Example: Defining maximum number of connections in the JAD file.

MIDlet-1: Foo, , Foo
MIDlet-Jar-Size: 7063
MIDlet-Jar-URL: foo.jar
MIDlet-Name: Foo
MIDlet-Vendor: Yoyodyne Inc.
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0
Device-Max-Connection: 3 
 

Example: Reading max number of connections from the JAD file.

    /* Initialization code */

    String deviceMaxConnectionString = getAppProperty("Device-Max-Connection");
    int deviceMaxConnection = Integer.parseInt(deviceMaxConnectionString);
    int openConnection = 0;
 

Example: Creating simultaneous network connections.

    if (openConnection < deviceMaxConnection) {
        HttpConnection connection = (HttpConnection) Connector.open(uri); 
        openConnection++;
    } else {
        // Fail to create connection.
    }
 

Example: Closing simultaneous network connections.

    connection.close();
    openConnection--;
 

Example: Counting the maximum number of connections.

    int connection = 0;
    try {
       while (true) {
          Connector.open("http://tools.ietf.org/html/rfc262" + connection);
       }
    } catch (Exception e) {
       // Max number reached here
    } 
 
Additional Information

None

3. List of Affected Devices

All devices

4. Keywords

Network, Multiple Connections, Browser, Email, IM, Network games, Mapping, Location

 
Copyright 2006 Sun Microsystems, Inc. and Orange SA All Rights Reserved.