Datagrams in J2ME (UDP Programming sample)

By David Hemphill Viewed: 31754 times Emailed: 193 times Printed: 212 times Bookmark and Share



It is quite possible for someone to have worked with Java for some time without ever needing to understand or use datagrams. For mobile devices, however, datagrams offer some advantages in that they are rather lightweight when compared to TCP-based connections such as sockets. User Datagram Protocol, UDP, is one of the more common datagram protocols. However, because most datagram protocols follow the same basic principals as to their usage, the GCF is able to support datagrams generically.

Datagrams are based on a connection-less paradigm, which means that a conversation is not established between two systems. Instead, datagrams are blindly transmitted across a network connection. To transmit a datagram from one system to another, an application creates a datagram and sends it to the intended target. This is a rather simple process in J2ME. However, there is a caveat to using datagrams in that they offer no guarantee or acknowledgement as to whether the datagram actually reached the intended recipient. As a result, a datagram implementation must either not care if the recipient actually receives the data 100 percent of the time, or an acknowledgement/handshake must be implemented manually by the application.

Using Datagrams
The following example shows how to create a datagram and send it to a specific IP address.


try	{
  DatagramConnection dgc = (DatagramConnection)
    Connector.open("datagram://localhost:9001");
  try {
    byte[] payload = "Test Message".getBytes();
    Datagram datagram = 
      dgc.newDatagram(payload, payload.length);
    dgc.send(datagram);
  } finally {
      dgc.close();
  }
} catch (IOException x) {
  x.printStackTrace();
}
In this example, a MIDlet creates a Datagram containing the text "Hello from a Datagram" and sends it to an application listening for datagrams on the local machine on port 9001. This example will execute just fine even if there is no application running to receive the datagram, thus demonstrating the connection-less nature of datagrams. The sender has no indication that the datagram was consumed by the target system.

Next I show how an application might receive the datagram sent by the previous code snippet.


try {
  DatagramConnection dgc = (DatagramConnection) 
    Connector.open("datagram://:9001");
  try {
    int size = 100;
    Datagram datagram = dgc.newDatagram(size);
    dgc.receive(datagram);
    System.out.println(
      new String(datagram.getData()).trim());
  } finally {
      dgc.close();
  }
} catch (IOException x){
  x.printStackTrace();
}
In the receive example, a datagram connection is established on port 9001. A datagram is created with a size of 100 bytes; if a received datagram contains more than 100 bytes, any data beyond the 100-byte mark is ignored. Once the datagram is created the receive() method is called, putting the thread in a wait state until a datagram is received. When a datagram is received, the payload is extracted from the datagram container and printed to the console.

In order to run the send and the receive examples, two instances of a MIDlet can be run from the Wireless Toolkit, one to perform the receive and one to perform the send.




Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-07-30]Code sample to Send SMS from a J2ME application.
[2009-05-29]Adding your own Application icon for your J2ME application (jar file)
[2008-08-18]Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
[2008-08-01]Datagrams in J2ME (UDP Programming sample)
[2008-08-01]Client Server in J2ME (Socket Programming sample)
[2008-08-01]Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
[2008-08-01]Using HTTP vs UDP vs Socket in J2ME
[2008-08-01]RMSCookieConnector - Using Cookies in J2ME
[2008-07-29]POST UTF-8 encoded data to the server in J2ME
[2008-07-10]lists, forms, choices, gauges, text fields, text boxes in J2ME
[2008-07-10]Using List to create a Menu and Menu items in J2ME
[2008-07-10]Using alerts and tickers in J2ME
[2008-07-07]J2ME Canvas sample to show games programming in J2ME
[2008-07-07]Timer and TimerTask example in J2ME
[2008-06-27]List of GPRS Access points for all countries

More Latest News

Most Viewed Articles (in last 30 days)
GUI components and menu based J2ME Applications.
Client Server in J2ME (Socket Programming sample)
Getting Started with J2ME
J2ME Canvas sample to show games programming in J2ME
RMS Basics in J2ME
Code sample to Send SMS from a J2ME application.
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
TextBox sample program in J2ME
'double buffering' Sample program in J2ME
Using List to create a Menu and Menu items in J2ME
Timer and TimerTask example in J2ME
Adding your own Application icon for your J2ME application (jar file)
What is J2ME?
Sample J2ME code that shows various functionality of RMS.
lists, forms, choices, gauges, text fields, text boxes in J2ME
Most Emailed Articles (in last 30 days)
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Getting Started with J2ME
Y.S. Sun Green Building Research Center
Sample J2ME code that shows various functionality of RMS.
Sample Java program shows how to Read a file over a network using J2ME midlet
Types of configurations in J2ME
'LINK.EXE' is not recognized as an internal or ext
RMS Basics in J2ME
GUI components and menu based J2ME Applications.
Your first J2ME program and a midlet lifecycle explained.
The MIDP Networking Model in J2ME
What is J2ME?
paint() sample program to draw a line in J2ME