The registration task involves the following actions:
Opening a basic TCP socket to the registration internet-working protocol and port
Constructing the XML registration message
Sending the XML registration message on the socket
Reading the XML reply message off the socket
Closing the socket
The following example code shows the implementation of the registerCallbacks method of the CrnpClient class (which is called by the CrnpClient constructor). The calls to createRegistrationString() and readRegistrationReply() are described in more detail later in this chapter.
regIp and regPort are object members that are set up by the constructor.
private void registerCallbacks() throws Exception { Socket sock = new Socket(regIp, regPort); String xmlStr = createRegistrationString(); PrintStream ps = new PrintStream(sock.getOutputStream()); ps.print(xmlStr); readRegistrationReply(sock.getInputStream(); sock.close(); }
This method is called by the shutdown method of CrnpClient. The implementation of createUnregistrationString is described in more detail later in this chapter.
private void unregister() throws Exception { Socket sock = new Socket(regIp, regPort); String xmlStr = createUnregistrationString(); PrintStream ps = new PrintStream(sock.getOutputStream()); ps.print(xmlStr); readRegistrationReply(sock.getInputStream()); sock.close(); }