Java Me Application Design Guide for multiple network connections

Source: Internet
Author: User

Some devices do not allow multiple network connections to be turned on at the same time, but sometimes our applications may need to open multiple connections at the same time. This can improve efficiency or the user experience, but the application should try to avoid relying on a multiple connection feature because your program might crash for devices that do not support multiple connections.

The solution to this problem is to mark the number of connections that the device can open simultaneously in the Jad file.

So how to get the maximum number of connections supported by the device, first you can refer to the specification of the device. For example, Motorola's device specification generally lists the types of connections that can be supported, such as HTTP sockets. And the maximum number of connections that the device can open at the same time. For what is not stated in the specification, we can only write a test MIDlet to test this feature first. Then write him to the Jad file for later use.

For example, the number of maximum connections is marked in the following 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

This quantity can be read when the application is initialized, and note that this quantity should be stored as a global variable so that it is accessible to people. Initialize the work as follows,

  /* Initialization code */
   String deviceMaxConnectionString = getAppProperty
("Device-Max-Connection");
   int deviceMaxConnection = Integer.parseInt
(deviceMaxConnectionString);
   int openConnection = 0;
同时创建多个网路连接,如下:
   if (openConnection < deviceMaxConnection) {
     HttpConnection connection = (HttpConnection) Connector.open
(uri);
     openConnection++;
   } else {
     // Fail to create connection.
   }

Turn off network connections

Connection.close (); openconnection--;

Calculates the 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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.