Next: , Previous: , Up: posix socket   [Index]


4.15.8 Examples of clients and servers

Basically a TCP/IP server should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let ((sockaddr    (px.make-sockaddr_in '#vu8(127 0 0 1) 8080))
      (server-sock (px.socket PF_INET SOCK_STREAM 0)))
  (px.bind   server-sock sockaddr)
  (px.listen server-sock 2)
  (let-values (((sock client-address)
                (px.accept server-sock)))
    ;; ... talk with the client through SOCK ...
    (px.close sock))
  (px.close server-sock))

while a TCP/IP client should to:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let ((sockaddr (px.make-sockaddr_in '#vu8(127 0 0 1) 8080))
      (sock     (px.socket PF_INET SOCK_STREAM 0)))
  (px.connect sock sockaddr)
  ;; ... talk to the server through SOCK ...
  (px.close sock))

A UDP/IP “server” should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let ((sockaddr   (px.make-sockaddr_in '#vu8(127 0 0 1) 8080))
      (sock       (px.socket PF_INET SOCK_DGRAM 0))
      (in-buffer  (make-bytevector 1024))
      (out-buffer (make-bytevector 1024)))
  (px.bind sock sockaddr)
  (let-values (((len sockaddr)
                (px.recvfrom sock in-buffer #f 0)))
    ;; ... process the received packet ...
    ;; ... prepare the answer ...
    (px.sendto sock out-buffer #f 0 sockaddr))
  (px.close sock))

while a UDP/IP “client” should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let ((sockaddr   (px.make-sockaddr_in '#vu8(127 0 0 1) 8081))
      (sock       (px.socket PF_INET SOCK_DGRAM 0))
      (out-buffer (make-bytevector 1024))
      (in-buffer  (make-bytevector 1024)))
  (px.bind sock sockaddr)
  ;; ... prepare the request ...
  (px.sendto sock out-buffer #f 0 sockaddr)
  (let-values (((len sockaddr)
                (px.recvfrom sock in-buffer #f 0)))
    ;; ... process the received packet ...
    )
  (px.close sock))

A Unix–domain socket server should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let* ((tmpdir       (px.getenv "TMPDIR"))
       (pathname     (string-append tmpdir "/use-me"))
       (sockaddr     (px.make-sockaddr_un pathname))
       (server-sock  (px.socket PF_LOCAL SOCK_STREAM 0)))
  (px.bind   server-sock sockaddr)
  (px.listen server-sock 2)
  (let-values (((sock client-address)
                (px.accept server-sock)))
    ;; ... talk to the client through SOCK ...
    (px.close sock))
  (px.close server-sock))

while a Unix–domain socket client should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let* ((tmpdir    (px.getenv "TMPDIR"))
       (pathname  (string-append tmpdir "/use-me"))
       (sockaddr  (px.make-sockaddr_un pathname))
       (sock      (px.socket PF_LOCAL SOCK_STREAM 0)))
  (px.connect sock sockaddr)
  ;; ... talk to the server through SOCK ...
  (px.close sock))

To receive incoming data and Out Of Band data, a TCP/IP server should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let ((sockaddr    (px.make-sockaddr_in '#vu8(127 0 0 1) 8080))
      (server-sock (px.socket PF_INET SOCK_STREAM 0)))
  (px.bind   server-sock sockaddr)
  (px.listen server-sock 2)
  (let*-values
      (((sock client-address) (px.accept server-sock))
       ((bv)                  (make-bytevector 10)))
    (let loop ()
      (let-values (((rd wr ex) (px.select-fd sock 2 0)))
        (cond (ex ;process exceptional conditions first!!!
               ;; block until OOB data arrives
               (let ((len (px.recv sock bv #f MSG_OOB)))
                 ;; process data in BV between 0 and LEN,
                 ;; which should be 1
                 (loop)))
              (rd
               (let ((len (px.recv sock bv #f 0)))
                 (when (positive? len)
                   ;; process data in BV between 0
                   ;; and LEN
                   (loop))))
              (else (loop)))))
    (px.close sock))
  (px.close server-sock))

to send outgoing data and Out Of Band data, a TCP/IP client should do:

#!r6rs
(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let ((sockaddr  (px.make-sockaddr_in '#vu8(127 0 0 1) 8080))
      (sock      (px.socket PF_INET SOCK_STREAM 0)))
  (px.connect sock sockaddr)
  (px.send sock '#vu8(1 2 3) #f 0)
  ;; only the single byte 6 (the last) is sent as
  ;; OOB data, 4 and 5 are sent as ordinary data
  (px.send sock '#vu8(4 5 6) #f MSG_OOB)
  (px.send sock '#vu8(7 8 9) #f 0)
  (px.close sock))

Next: , Previous: , Up: posix socket   [Index]