Java Reference
In-Depth Information
The socket options are a subset of the socket options you may have encountered
using TCP sockets on Linux or UNIX; Table 12-3 summarizes the options you can obtain
or mutate. You pass the option identifier to getSocketOption , and it returns the resulting
option; you pass the option identifier and a new value to setSocketOption , and it attempts
to set the option you specify.
Table 12-3. Socket Options
Option Identifier
Meaning
SocketConnection.DELAY
Duration to delay when writing to small buffers
SocketConnection.KEEPALIVE
Duration that a socket should remain open for keep-alive
purposes
SocketConnection.LINGER
Duration that a socket should linger open with output
remaining when there's no response from the other side of
the socket
SocketConnection.RCVBUF
Size in bytes of the receiving buffer
SocketConnection.SNDBUF
Size in bytes of the sending buffer
The GCF also has support for accepting incoming socket requests through the
StreamConnectionNotifier and ServerSocketConnection classes, although in practice Java ME
applications rarely use this facility. There are at least three reasons why you probably don't
want a Java ME application listening on a socket for incoming connections. First, listening
for an incoming connection requires the hardware to supply power to the network subsys-
tem. For many portable devices, especially those with wireless radios, this can drastically
reduce battery life. Second, the networks on which most Java ME devices operate typically
use dynamic address assignment when allocating addresses to Java ME devices; even if
there weren't energy penalties for operating the network hardware, you wouldn't know what
address corresponded to a specific device without using some kind of registration mecha-
nism in advance. Finally, only a small number of devices offer this support.
If you're sure these reasons don't apply to you, you can obtain a ServerSocketConnection
by simply specifying the port on which you want to listen, as shown in Listing 12-3.
Listing 12-3. Obtaining a ServerSocketConnection
String url = "socket://7";
ServerSocketConnection ssc = null;
try {
ssc = (ServerSocketConnection)Connector.open(url);
while (true) {
SocketConnection sc = (SocketConnection)ssc.acceptAndOpen();
InputStream s = sc.openInputStream();
OutputStream o = sc.openOutputStream();
 
Search WWH ::




Custom Search