This SRFI is currently in ``draft'' status. To see an explanation of
each status that a SRFI can hold, see here.
To provide input on this SRFI, please
mail to
<srfi minus 106 at srfi dot schemers dot org>
. See
instructions here to
subscribe to the list. You can access previous messages via
the archive of the mailing list.
Many Scheme implementation have its own socket APIs however there are no portable way to write socket program. Therefore programmers need to provide implementation dependent layer for their program.
This document specifies high and middle range of socket interfaces which are commonly used to write socket programming. That should make it easier to write portable programs that need to send or receive data from their socket.
make-client-socket make-server-socket socket?
socket-accept socket-send socket-recv socket-shutdown socket-close
socket-port
call-with-socket
address-family address-info socket-domain ip-protocol message-type shutdown-method socket-merge-flags socket-parge-flags
*af-unspec* *af-inet* *af-inet6* *sock-stream* *sock-dgram* *ai-canoname* *ai-numerichost* *ai-v4mapped* *ai-all* *ai-addrconfig* *ipproto-ip* *ipproto-tcp* *ipproto-udp* *msg-peek* *msg-oob* *msg-waitall* *shut-rd* *shut-wr* *shut-rdwr*
socket | a socket object |
bv | a bytevector |
object | any value |
"localhost" "127.0.0.1"
"http" "80"
*af-inet*
*sock-stream*
(socket-merge-flags *ai-v4mapped* *ai-addrconfig*)
*ipproto-ip*
make-client-socket
.
*af-inet*
*sock-stream*
*ipproto-ip*
(message-type none)
(message-type none)
call-with-port
.
inet
*af-inet*
inet6
*af-inet6*
unspec
*af-unspec*
unix
or
local
or other names.
canoname
*ai-canoname*
numerichost
*ai-numerichost*
v4mapped
*ai-v4mapped*
all
*ai-all*
addrconfig
*ai-addrconfig*
stream
*sock-stream*
datagram
*sock-dgram*
ip
*ipproto-ip*
tcp
*ipproto-tcp*
udp
*ipproto-udp*
socket-recv
and
socket-send
.none
peek
*msg-peek*
oob
*msg-oob*
wait-all
*msg-waitall*
read
*shut-rd*
write
*shut-wr*
shutdown-method
is given both read
and
write
, then it must return *shut-rdwr*
AF_INET
.
AF_INET6
.
AF_UNSPEC
.
SOCK_STREAM
.
SOCK_DGRAM
.
AI_CANONNAME
.
AI_NUMERICHOST
.
AI_V4MAPPED
.
AI_ALL
.
AI_ADDRCONFIG
.
IPPROTO_IP
.
IPPROTO_TCP
.
IPPROTO_UDP
.
socket-recv
.socket-recv
shall still return this data.MSG_PEEK
.
socket-recv
and socket-send
.MSG_OOB
.
socket-recv
.*sock-stream*
flag, this requests
the procedure block until the full amount of data ban be returned.MSG_WAITALL
.
SHUT_RD
.
SHUT_WR
.
SHUT_RDWR
.
Implementation name | Condition type |
---|---|
Gauche | <system-error> |
Larceny | no error, returns #f |
Mosh | &i/o-read |
Racket | exn:fail:network |
Sagittarius | &i/o |
Ypsilon | &i/o |
;; simple echo server (import (rnrs) (srfi :106 socket)) (define echo-server-socket (make-server-socket "5000")) (define (server-run) (define (get-line-from-binary-port bin) (utf8->string (call-with-bytevector-output-port (lambda (out) (let loop ((b (get-u8 bin))) (case b ((#xA) #t) ;; newline ((#xD) (loop (get-u8 bin))) ;; carriage return (else (put-u8 out b) (loop (get-u8 bin)))))))) (let ((addr (socket-accept echo-server-socket))) (call-with-socket addr (lambda (sock) (let ((p (socket-port sock))) (call-with-port p (lambda (p) (let lp2 ((r (get-line-from-binary-port p))) (put-bytevector p (string->utf8 (string-append r "\r\n")) (lp2 (get-line-from-binary-port p)))))))))) (server-run) ;; simple echo client (import (rnrs) (srfi :106 socket)) (define client-socket (make-client-socket "localhost" "5000" (address-family inet) (socket-domain stream) (address-info v4mapped addrconfig) (ip-protocol ip))) (socket-send client-socket (string->utf8 "hello\r\n")) (socket-recv client-socket (string-length "hello\r\n"))
(library (srfi :106 socket) (export make-client-socket make-server-socket socket? socket-port call-with-socket socket-merge-flags socket-parge-flags socket-accept socket-send socket-recv socket-shutdown socket-close *af-unspec* *af-inet* *af-inet6* *sock-stream* *sock-dgram* *ai-canoname* *ai-numerichost* *ai-v4mapped* *ai-all* *ai-addrconfig* *ipproto-ip* *ipproto-tcp* *ipproto-udp* *shut-rd* *shut-wr* *shut-rdwr* address-family socket-domain address-info ip-protocol message-type shutdown-method) (import (rnrs) (rename (socket impl) (socket-port %socket-port))) (define %address-family `((inet ,*af-inet*) (inet6 ,*af-inet6*) (unspec ,*af-unspec*))) (define %address-info `((canoname ,*ai-canoname*) (numerichost ,*ai-numerichost*) (v4mapped ,*ai-v4mapped*) (all ,*ai-all*) (addrconfig ,*ai-addrconfig*))) (define %ip-protocol `((ip ,*ipproto-ip*) (tcp ,*ipproto-tcp*) (udp ,*ipproto-udp*))) (define %socket-domain `((stream ,*sock-stream*) (datagram ,*sock-dgram*))) (define %message-types `((none 0) (peek ,*msg-peek*) (oob ,*msg-oob*) (wait-all ,*msg-waitall*))) (define (lookup who sets name) (cond ((assq name sets) => cadr) (else (assertion-violation who "no name defined" name)))) (define-syntax address-family (syntax-rules () ((_ name) (lookup 'address-family %address-family 'name)))) (define-syntax address-info (syntax-rules () ((_ names ...) (apply socket-merge-flags (map (lambda (name) (lookup 'address-info %address-info name)) '(names ...)))))) (define-syntax socket-domain (syntax-rules () ((_ name) (lookup 'socket-domain %socket-domain 'name)))) (define-syntax ip-protocol (syntax-rules () ((_ name) (lookup 'ip-protocol %ip-protocol 'name)))) (define-syntax message-type (syntax-rules () ((_ names ...) (apply socket-merge-flags (map (lambda (name) (lookup 'message-type %message-types name)) '(names ...)))))) (define (%proper-method methods) (define allowed-methods '(read write)) (define (check-methods methods) (let loop ((methods methods) (seen '())) (cond ((null? methods)) ((memq (car methods) allowed-methods) => (lambda (m) (if (memq (car m) seen) (assertion-violation 'shutdown-method "duplicate method" m) (loop (cdr methods) (cons (car m) seen))))) (else (assertion-violation 'shutdown-method "unknown method" (car methods)))))) (check-methods methods) (if (null? (cdr methods)) (case (car methods) ((read) *shut-rd*) ((write) *shut-wr*)) *shut-rdwr*)) (define-syntax shutdown-method (syntax-rules () ((_ methods ...) (%proper-method '(methods ...))))) (define (socket-port socket . close?) (define (read! bv start count) (let ((r (socket-recv socket count))) (bytevector-copy! r 0 bv start (bytevector-length r)) (bytevector-length r))) (define (write! bv start count) (let ((buf (make-bytevector count))) (bytevector-copy! bv start buf 0 count) (socket-send socket buf))) (if (or (null? close?) (not (car close?))) (make-custom-binary-input/output-port "socket-port" read! write! #f #f #f) (%socket-port socket))) )
(library (srfi :106 socket impl) (export make-client-socket make-server-socket socket? socket-port call-with-socket (rename (bitwise-ior socket-merge-flags) (bitwise-xor socket-parge-flags)) socket-accept socket-send socket-recv socket-shutdown socket-close (rename (AF_UNSPEC *af-unspec*) (AF_INET *af-inet*) (AF_INET6 *af-inet6*)) (rename (SOCK_STREAM *sock-stream*) (SOCK_DGRAM *sock-dgram*)) (rename (AI_CANONNAME *ai-canoname*) (AI_NUMERICHOST *ai-numerichost*) (AI_V4MAPPED *ai-v4mapped*) (AI_ALL *ai-all*) (AI_ADDRCONFIG *ai-addrconfig*)) (rename (IPPROTO_IP *ipproto-ip*) (IPPROTO_TCP *ipproto-tcp*) (IPPROTO_UDP *ipproto-udp*)) (rename (MSG_PEEK *msg-peek*) (MSG_OOB *msg-oob*) (MSG_WAITALL *msg-waitall*)) (rename (SHUT_RD *shut-rd*) (SHUT_WR *shut-wr*) (SHUT_RDWR *shut-rdwr*))) (import (rnrs) (rename (ypsilon socket) (socket-send %socket-send) (socket-recv %socket-recv))) (define IPPROTO_IP 0) (define IPPROTO_TCP 6) (define IPPROTO_UDP 17) (define (socket-send socket bv . flags) (let ((flags (if (null? flags) 0 (car flags)))) (%socket-send socket bv flags))) (define (socket-recv socket size . flags) (let ((flags (if (null? flags) 0 (car flags)))) (%socket-recv socket size flags))) )
(library (srfi :106 socket impl) (export make-client-socket make-server-socket socket? socket-port call-with-socket (rename (bitwise-ior socket-merge-flags) (bitwise-xor socket-parge-flags)) socket-accept socket-send socket-recv socket-shutdown socket-close (rename (AF_UNSPEC *af-unspec*) (AF_INET *af-inet*) (AF_INET6 *af-inet6*)) (rename (SOCK_STREAM *sock-stream*) (SOCK_DGRAM *sock-dgram*)) (rename (AI_CANONNAME *ai-canoname*) (AI_NUMERICHOST *ai-numerichost*) (AI_V4MAPPED *ai-v4mapped*) (AI_ALL *ai-all*) (AI_ADDRCONFIG *ai-addrconfig*)) (rename (IPPROTO_IP *ipproto-ip*) (IPPROTO_TCP *ipproto-tcp*) (IPPROTO_UDP *ipproto-udp*)) (rename (MSG_PEEK *msg-peek*) (MSG_OOB *msg-oob*) (MSG_WAITALL *msg-waitall*)) (rename (SHUT_RD *shut-rd*) (SHUT_WR *shut-wr*) (SHUT_RDWR *shut-rdwr*))) (import (rnrs) (mosh socket)) (define IPPROTO_IP 0) (define MSG_OOB 1) (define MSG_PEEK 2) (define MSG_WAITALL 8) )
(library (srfi :106 socket impl) (export make-client-socket make-server-socket socket? socket-port call-with-socket (rename (bitwise-ior socket-merge-flags) (bitwise-xor socket-parge-flags)) socket-accept socket-send socket-recv socket-shutdown socket-close (rename (AF_UNSPEC *af-unspec*) (AF_INET *af-inet*) (AF_INET6 *af-inet6*)) (rename (SOCK_STREAM *sock-stream*) (SOCK_DGRAM *sock-dgram*)) (rename (AI_CANONNAME *ai-canoname*) (AI_NUMERICHOST *ai-numerichost*) (AI_V4MAPPED *ai-v4mapped*) (AI_ALL *ai-all*) (AI_ADDRCONFIG *ai-addrconfig*)) (rename (IPPROTO_IP *ipproto-ip*) (IPPROTO_TCP *ipproto-tcp*) (IPPROTO_UDP *ipproto-udp*)) (rename (MSG_PEEK *msg-peek*) (MSG_OOB *msg-oob*) (MSG_WAITALL *msg-waitall*)) (rename (SHUT_RD *shut-rd*) (SHUT_WR *shut-wr*) (SHUT_RDWR *shut-rdwr*))) (import (rnrs) (rename (sagittarius socket) (socket-send %socket-send) (socket-recv %socket-recv))) (define IPPROTO_IP 0) (define IPPROTO_TCP 6) (define IPPROTO_UDP 17) (define (socket-send socket bv :optional (flags 0)) (%socket-send socket bv flags)) (define (socket-recv socket size :optional (flags 0)) (%socket-recv socket size flags)) )
(library (srfi :106 socket impl) (export make-client-socket make-server-socket socket? socket-port call-with-socket socket-accept socket-send socket-recv socket-shutdown socket-close (rename (AF_UNSPEC *af-unspec*) (AF_INET *af-inet*) (AF_INET6 *af-inet6*)) (rename (SOCK_STREAM *sock-stream*) (SOCK_DGRAM *sock-dgram*)) (rename (AI_CANONNAME *ai-canoname*) (AI_NUMERICHOST *ai-numerichost*) (AI_V4MAPPED *ai-v4mapped*) (AI_ALL *ai-all*) (AI_ADDRCONFIG *ai-addrconfig*)) (rename (IPPROTO_IP *ipproto-ip*) (IPPROTO_TCP *ipproto-tcp*) (IPPROTO_UDP *ipproto-udp*)) (rename (MSG_PEEK *msg-peek*) (MSG_OOB *msg-oob*) (MSG_WAITALL *msg-waitall*)) (rename (SHUT_RD *shut-rd*) (SHUT_WR *shut-wr*) (SHUT_RDWR *shut-rdwr*))) (import (rnrs)) (define-syntax define-unsupported (syntax-rules () ((_ (name)) (define (name . _) (raise (condition (make-implementation-restriction-violation) (make-who-condition 'name) (make-message-condition "This SRFI is not supported on this implementation"))))) ((_ name) (define name #f)))) (define-unsupported (make-client-socket)) (define-unsupported (make-server-socket)) (define-unsupported (socket? )) (define-unsupported (socket-port )) (define-unsupported (call-with-socket )) (define-unsupported (socket-accept )) (define-unsupported (socket-send )) (define-unsupported (socket-recv )) (define-unsupported (socket-shutdown )) (define-unsupported (socket-close )) (define-unsupported AF_UNSPEC ) (define-unsupported AF_INET ) (define-unsupported AF_INET6 ) (define-unsupported SOCK_STREAM ) (define-unsupported SOCK_DGRAM ) (define-unsupported AI_CANONNAME ) (define-unsupported AI_NUMERICHOST) (define-unsupported AI_V4MAPPED ) (define-unsupported AI_ALL ) (define-unsupported AI_ADDRCONFIG ) (define-unsupported IPPROTO_IP) (define-unsupported IPPROTO_TCP) (define-unsupported IPPROTO_UDP) (define-unsupported MSG_OOB) (define-unsupported MSG_PEEK) (define-unsupported MSG_WAITALL) (define-unsupported SHUT_RD) (define-unsupported SHUT_WR) (define-unsupported SHUT_RDWR) )
[1] The Open Group Base Specifications Issue 7 http://pubs.opengroup.org/onlinepubs/9699919799/nframe.html
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.