By: David Hemphill in J2ME Tutorials on 2008年08月01日 [フレーム]
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in J2ME )
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Client Server in J2ME (Socket Programming sample)
GUI components and menu based J2ME Applications.
Code sample to Send SMS from a J2ME application.
Datagrams in J2ME (UDP Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
RMSCookieConnector - Using Cookies in J2ME
Using HTTP vs UDP vs Socket in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Latest Articles (in J2ME)
GUI components and menu based J2ME Applications.
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
GUI components and menu based J2ME Applications.
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate