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
socket-merge-flags socket-parge-flags
AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG IPPROTO_IP IPPROTO_TCP IPPROTO_UDP 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
call-with-port
.
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 |
(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 (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_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG IPPROTO_IP IPPROTO_TCP IPPROTO_UDP SHUT_RD SHUT_WR SHUT_RDWR) (import (srfi :106 socket impl)))
(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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG IPPROTO_IP IPPROTO_TCP IPPROTO_UDP SHUT_RD SHUT_WR 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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG IPPROTO_IP IPPROTO_TCP IPPROTO_UDP SHUT_RD SHUT_WR SHUT_RDWR) (import (rnrs) (mosh socket) (define IPPROTO_IP 0) )
(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 AF_UNSPEC AF_INET AF_INET6 SOCK_STREAM SOCK_DGRAM AI_PASSIVE AI_CANONNAME AI_NUMERICHOST AI_V4MAPPED AI_ALL AI_ADDRCONFIG IPPROTO_IP IPPROTO_TCP IPPROTO_UDP SHUT_RD SHUT_WR 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 (rename (bitwise-ior socket-merge-flags) (bitwise-xor socket-parge-flags)) 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 IPPROTO_IP IPPROTO_TCP IPPROTO_UDP 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 IPPROTO_IP) (define-unsupported IPPROTO_TCP) (define-unsupported IPPROTO_UDP) (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.