Datagrams

In addition to stream sockets, the CLDC reference implementation contains support for datagrams. Datagrams and stream sockets differ in several ways:

Data stream versus message passing

A stream socket sends a continuous stream of data from a sender to a receiver, with no provision for marking record boundaries. Datagrams are sent in discrete packets; data sent in one packet is never delivered in the same read as data from another packet.

Connection-oriented versus connectionless

When a stream socket is used, a connection, along which all the data flows, is created between the sender and the receiver. As a result, there is no need to specify where each message is going. A datagram socket does not use a connection; each message is individually addressed, and different messages may go to different destinations. Likewise, a datagram socket may receive messages from any number of different sources, but a stream socket receives data from only a single sender.

Reliability

Data sent using a stream socket is guaranteed to be delivered to the receiver, unless the intervening network fails. In that case, the receiver is notified that its connection to the sender has been lost. Furthermore, the individual bytes are delivered in the order in which they were sent, without duplication. A datagram socket makes no such promises. Messages may be lost or duplicated, or may not arrive in the order in which they were sent.

Because they do not incur the relatively large cost of setting up a ...

Get J2ME in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.