Design Article

Mobile Game Networking Essentials--Part III

Michael Morrison

6/20/2008 11:54 AM EDT

See Part I and Part II

Network Programming and J2ME
Network programming in MIDlets is carried out using a portion of the MIDP API known as the Generic Connection Framework, or GCF. The purpose of the GCF is to provide a level of abstraction for networking services, which helps in enabling different mobile devices to support only network protocols specific to their needs.

Although it is structured somewhat differently, the GCF is implemented as a functional subset of the J2SE API. The GCF describes one fundamental class named Connector that is used to establish all MIDlet network connections. Specific types of network connections are modeled by interfaces that are obtained through the Connector class. The Connector class and the connection interfaces are located in the javax.microedition.io package. Descriptions of a few of these interfaces follow:


From the perspective of mobile game programming, you will typically use either the DatagramConnection or StreamConnection interfaces as the network connection types for games. You always use the Connector class to establish network connections, regardless of the connection type. All the methods in the Connector class are static, with the most important one being the open ( ) method. The most commonly used version of the open ( ) method follows:


The parameter to this method is the connection string, which determines the type of connection being made. The connection string describes the connection by adhering to the following general form:


The Scheme parameter is the name of the network protocol, such as http, ftp, or datagram. The Target parameter is typically the name of the network address for the connection, but can vary according to the specific protocol. The last parameter, Parameters, is a list of parameters associated with the connection. Some examples of different types of connection strings for various network connections are:



Keep in mind that although these examples are accurate in terms of describing possible connection strings, the only one of them that has guaranteed support in a given MIDP implementation is the first one. The MIDP specification requires an implementation only to support HTTP connections. If you happen to know that a given MIDP implementation supports a particular type of connection, then you can certainly take advantage of it. Otherwise, you will need to stick with HTTP connections, which admittedly aren't too useful in networked mobile games.

The open ( ) method returns an object of type Connection, which is the base interface for all the other connection interfaces. To use a certain kind of connection interface, you cast the Connection interface returned by open ( ) to the appropriate type. The following line of code illustrates how to use the DatagramConnection interface to open a datagram connection:


Construction Cue: The number 5555in this example code is the network port used by the datagram connection. This port number can be any value over 1024, but it's very important for the client and server code in a networked MIDlet to communicate through the same port number.

The next few sections dig a little deeper into datagram connections, and how to send and receive data through them.


Next:




Please sign in to post comment

Navigate to related information

Datasheets.com Parts Search

185 million searchable parts
(please enter a part number or hit search to begin)

Feedback Form