next up previous
Next: Graphics (windows.ccl) Up: Standard Libraries Previous: Interprocess Communication (iproc.ccl)

UDP Datagrams (udp.ccl)

The udp.ccl library allows CCL programs to communicate with other CCL programs running as truely separate processes (started with a different call to ccli), possibly on other machines. The library is similar to the iproc.ccl library, except that explicit servers and clients are used. The library consists of five functions. The first three are used by UDP servers.


external int udp_new_server ( int )
The argument to this function is the UDP port the server should listen to. Port numbers in the 7000s are good for experimental programs. The return value is an opaque integer server id (sid) that is used in udp_is_ready and udp_get_data.


external bool udp_is_ready ( int )
The argument to this function is the sid of a server started with udp_new_server. It returns true if and only if there is data ready to receive in the server's buffer. It does not block.


external [ timestamp := int, from := string, data := 'a ]
udp_get_data ( int )

The argument to this function is a valid sid. The return value is a record that has three fields: an integer timestamp field that can be used to order messages from the same client in terms of the time they were sent; a string valued from field containing the host name of the client that sent the data; and a data field whose type depends on what the client sent.


An example server that simply prints to the terminal the messages it receives is as follows.

include standard.ccl
include udp.ccl

program server ( port ) := {

  sid := udp_new_server ( port );

  udp_is_ready ( sid ) : {
    print ( "got: ", udp_get_data ( sid ), 
            " on port ", port, "\r" )
  }

};

program main() := server ( 8000 );
The other two udp.ccl functions are for clients.


external int udp_new_client ( string, int )
The first argument to this function is a string containing the hostname to which to send data and the second argument is the UDP port to address. The return value is the client id (cid) that should be used in calls to udp_send_data.


external unit udp_send_data ( int, 'a )
The first argument to this function is a valid cid. The second argument is the data of the message and may of any type.


Once again, care should be used with the data field as ccli can not type check whether the client's idea of the type of the data is consistent with the server's idea of what it is. An example client that can communicate with the above server is as follows.

include standard.ccl
include udp.ccl

program client ( host, port, period ) := {

  cid := udp_new_client ( host, port );
  count := 0;
  time := dclock();

  dclock() >= time + period  : {
    time := dclock(),
    udp_send_data ( cid, [ str   := "this some data", 
                           count := count ] ),
    count := count + 1
  }

};

hostname := if ARGC > 2 then ARGV[2] else "localhost" end;
program main() := client ( hostname, 8000, 0.25 );


next up previous
Next: Graphics (windows.ccl) Up: Standard Libraries Previous: Interprocess Communication (iproc.ccl)
Eric Klavins 2003-12-03