Java Reference
In-Depth Information
handler.out.write ('\r');
handler.out.write ('\n');
handler.out.flush();
sendField.setText ("");
}
else if (event.getSource() == connectButton) {
disconnect();
handler = new Handler (urlField.getText());
handler.start();
}
}
catch (Exception e) {
incoming.add(e.toString());
incoming.add("");
disconnect();
}
}
/** Closes the connection if any */
public void disconnect() {
if (handler != null) {
handler.leave = true;
show ("disconnected!\r");
try {
handler.connection.close();
handler.in.close();
handler.out.close();
}
catch (IOException e) {
}
handler = null;
}
}
public void pauseApp() {
}
public void destroyApp (boolean unconditional) {
disconnect();
frame.setVisible (false);
}
A Simple HTTP-Based Client-Server Chat Application
In addition to the telnet client, we would like to show you how to build a "real" client-server
application, where the server runs on a desktop computer, and the CLDC device takes over the role of
the client. To keep things simple, we have chosen a chat application as an example. The idea is that you
can connect to a server, see the messages from other people connected to the same server, and write
your own messages that become visible to the other users. Because HTTP is the only protocol available
for all devices, we will use HTTP as the communication protocol for our application. However, using
HTTP includes a significant drawback: HTTP does not provide server initiated transmissions, so the
clients need to connect to the server and to "poll" for new data from time to time. It might be possible
to work around this limitation by keeping an HTTP connection open for each client and to forward data
to all connected clients automatically. However, it might be possible that the gateway used by the
device for HTTP access does not support this, so we will stick to polling here.
In order to allow the clients to receive only their new messages, all messages have an unique number,
which is managed by a simple server-sided counter. Thus, the client can submit the number of the
 
Search WWH ::




Custom Search