Java Reference
In-Depth Information
try {
...
}
catch(Exception e) {...}
finally {
if (s!=null) s.close();
if (o!=null) o.close();
if (sc!=null) sc.close();
}
}
} catch (Exception e) {...}
finally {
try {
if (ssc != null) ssc.close();
} catch (Exception e) {...}
}
This code strongly resembles that of a traditional select/accept server loop written
using UNIX sockets. After creating a ServerSocketConnection , the code blocks on the
ServerSocketConnection instance's acceptAndOpen method, which returns a SocketConnection
instance once a client has connected. Next, it opens input and output streams using the
socket to perform the necessary I/O service; once this is done, it cleans up and goes back to
waiting for the next request.
Of course, the accept/respond loop only runs for the lifetime of your application; a
true server would likely need to listen even when your application is not running. On
MIDP platforms, you can accomplish this using the push registry by registering your
application for incoming requests either at installation time or at runtime. To register at
installation time, simply include the GCF URL describing the socket to which you want to
listen and the name of your MIDlet as a value for a MIDlet-Push field in the application
descriptor, like this:
MIDlet-Push-1: socket://:7, ServerMIDlet, *
This instructs the application management system to invoke your application
whenever an incoming TCP request appears on port 7. Your application can then use
the PushRegistry 's listConnections method—which I describe in Chapter 14—to obtain
a list of connections that have data waiting. You can also programmatically add your
application to the push registry by invoking its registerConnection method, passing
exactly the same information as you provide in the MIDlet-Push field of your applica-
tion descriptor.
 
Search WWH ::




Custom Search