5.5 Example: Are We Working Right Now?

As an example, we show how to use the finger service from Oz. Services are programs running on a host, where communication is provided by a socket connection. A well known example is the finger service.

Services use the Internet domain, thus for connecting to them we need the name of the host computer (i. e.its Internet address) and the port of the socket. For this reason there is the procedure OS.getServByName (see Chapter 21 of ``System Modules'') which gives the port of the socket on which the service is available. Feeding

FingerPort={OS.getServByName finger tcp}

binds FingerPort to the port number of the service.

Note that one has to specify the protocol the service uses, here the TCP protocol (as specified by the virtual string tcp). The port number of a service is unique for all computers, so this number does not depend on your localhost.

To get the information from this service we have to connect to the finger service. If you want to know whether the author is working right now, then you can connect to one of our computers with the Internet address wallaby.ps.uni-sb.de on the FingerPort. This is done by feeding the code shown in Program Program 5.2.

class FingerClient from Open.socket 
   meth getInfo($)
      Is Ir  
      if {self read(list:Is tail:Ir len:$)}==then nil
      else {self getInfo(Ir)} Is
FC = {New FingerClient client(host:'wallaby.ps.uni-sb.de'  

Program 5.2: The finger client FC.

For discovering whether the author is logged in, you have to send his username (in this example schulte) followed by a newline to the service, and then receive the information:

{FC write(vs:'schulte\n')}
{Browse {FC getInfo($)}}

For receiving the information we use the method getInfo which reads the entire information from the socket until it is closed (the finger service closes the connection automatically after sending its information).

Christian Schulte
Version 1.4.0 (20080702)