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 a uniform interface 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
AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG SHUT_RD SHUT_WR SHUT_RDWR
socket | a socket object |
bv | a bytevector |
object | any value |
"localhost" "127.0.0.1"
"http" "80"
make-client-socket
.
(import (rnrs) (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)
(library (socket) (export make-client-socket make-server-socket socket? socket-port call-with-socket socket-accept socket-send socket-recv socket-shutdown socket-close AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG SHUT_RD SHUT_WR SHUT_RDWR) (import (socket impl)))
(library (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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG SHUT_RD SHUT_WR SHUT_RDWR) (import (rnrs) (rename (ypsilon socket) (socket-send %socket-send) (socket-recv %socket-recv))) (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 (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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG SHUT_RD SHUT_WR SHUT_RDWR) (import (mosh socket))
(library (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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG SHUT_RD SHUT_WR SHUT_RDWR) (import (rnrs) (rename (sagittarius socket) (socket-send %socket-send) (socket-recv %socket-recv))) (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 (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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG SHUT_RD SHUT_WR 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_PASSIVE ) (define-unsupported AI_CANONNAME ) (define-unsupported AI_NUMERICHOST) (define-unsupported AI_V4MAPPED ) (define-unsupported AI_ALL ) (define-unsupported AI_ADDRCONFIG ) (define-unsupported SHUT_RD) (define-unsupported SHUT_WR) (define-unsupported SHUT_RDWR))
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.