by Daniel Pimentel — Thu 02 February 2017

GNU Guile-RPC is an implementation of ONC RPC and XDR (standardized as RFC 1831 and RFC 4506) in Guile Scheme, and for use by GNU Guile programs. ONC RPC is the ``Open Network Computing'' Remote Procedure Call protocol, allowing programs to invoke procedures of programs running on remote machines. XDR is the underlying binary data representation format.


;; client

(use-modules (rpc rpc))

(define invoke-split-number
  (make-synchronous-rpc-call 80000 0    ;; program and version
                             1          ;; procedure number
                             xdr-double ;; argument type

;; server
(use-modules (rpc rpc server))

(define (split-number-handler number)
  ;; Handle a `split-number' request.
  (let* ((int (floor number))
         (dec (floor (* 1000 (- number int)))))
    (list (inexact->exact int)
          (inexact->exact dec))))

(define my-rpc-program
  ;; Define our RPC program.
  (let* ((proc    (make-rpc-procedure 1 xdr-double result-type
         (version (make-rpc-program-version 0 (list proc))))
    (make-rpc-program 80000 (list version))))

;; TCP socket
;; Creating a listening TCP socket.
(define server-socket (socket PF_INET SOCK_STREAM 0))

;; Listen for connections on, port 6666.
(bind server-socket AF_INET INADDR_LOOPBACK 6666)
(listen server-socket 1024)

;; Go ahead and serve requests.
(run-stream-rpc-server (list (cons server-socket my-rpc-program))
                       1000000 ;; a one-second timeout
                       #f      ;; we don't care about closed connections
                       (lambda () ;; our idle thunk
                         (format #t "one second passed~%")))
2000-2016 by Daniel Pimentel under GFDL