(ypsilon socket) — Socket interface
(make-client-socket <node> <service> [<ai-family> <ai-socktype> <ai-flags> <ai-protocol>]) procedure
- Returns a client socket connected to an Internet address.
<node>is a network address — (ex. “www.w3.org”, “localhost”, “127.0.0.1”).<service>is a network service — (ex. “http”, “ssh”, “8080”).<ai-family>is an address family specifier eitherAF_INETAF_INET6AF_UNSPEC, default isAF_INET.<ai-socktype>is a socket type specifier eitherSOCK_STREAMSOCK_DGRAMSOCK_RAW, default isSOCK_STREAM.<ai-flags>is an additional options specifier combination ofAI_ADDRCONFIGAI_ALLAI_CANONNAMEAI_NUMERICHOSTAI_NUMERICSERVAI_PASSIVEAI_V4MAPPED, default isAI_V4MAPPED + AI_ADDRCONFIG.<ai-protocol>is a protocol specifier eitherIPPROTO_IPIPPROTO_TCPIPPROTO_UDPIPPROTO_RAW, default isIPPROTO_IP.
(import (rnrs) (ypsilon socket)) (make-client-socket "www.w3.org" "http") ;=> #<client-socket tcp stream 128.30.52.100:80>The Internet address is identified by node and service. make-client-socket uses getaddrinfo(3) to look up it. The arguments node, service, ai-family, ai-socktype, ai-flags, and ai-protocol will be passed to getaddrinfo(3) as a correspondent parameter. Refer to getaddrinfo(3) manual page for details.
(make-server-socket <service> [<ai-family> <ai-socktype> <ai-protocol>]) procedure
- Returns a server socket waiting for connections.
- Arguments
<service><ai-family><ai-socktype><ai-protocol>are same withmake-client-socket.
(import (rnrs) (ypsilon socket)) (make-server-socket "8080") ;=> #<server-socket tcp stream 0.0.0.0:8080>")))
(call-with-socket <socket> <procedure>) procedure
- Calls
<procedure>with<socket>as an argument. - This procedure has an analogy to
call-with-portof(rnrs io ports). - If
<procedure>returns, socket is closed implicitly, andcall-with-socketreturns a value returned by<procedure>.
(socket-port <socket>) procedure
- Returns a fresh binary input/output port associated with a socket.
(import (rnrs) (ypsilon socket)) (call-with-socket (make-client-socket "www.w3.org" "http") (lambda (socket) (call-with-port (transcoded-port (socket-port socket) (make-transcoder (utf-8-codec) (eol-style none))) (lambda (port) (put-string port "GET / HTTP/1.1\r\n") (put-string port "HOST: www.w3.org\r\n") (put-string port "\r\n") (display (get-string-all port)))))) ; => ; HTTP/1.1 200 OK ; date: Sat, 26 Feb 2022 06:31:32 GMT ; content-location: Home.html ; ...
(socket? <obj>) procedure
- Returns #t if
<obj>is a socket, and otherwise returns #f.
(socket-accept <socket>) procedure
- Wait for an incoming connection request, and returns a fresh connected client socket
(socket-send <socket> <bytevector> <flags>) procedure
- Sends a binary data block
<bytevector>to<socket>.socket-senduses send(2) to send data. The arguments<flags>will be passed to send(2) as a correspondent parameter. Refer to send(2) manual page for details.
(socket-recv <socket> <flags>) procedure
- Receives a binary data block from
<socket>and returns new bytevector contains it.socket-recvuses recv(2) to receive data. The arguments<flags>will be passed to recv(2) as a correspondent parameter. Refer to recv(2) manual page for details.
(socket-close <socket>) procedure
- Closes
<socket>.
(socket-shutdown <socket> <how>) procedure
- Shutdowns
<socket>. <how>is eitherSHUT_RDSHUT_WRSHUT_RDWR.
(shutdown-output-port <port>) procedure
- Flushes
<port>output, then shutdowns output connection of a socket that associated with<port>.
Constants
AF_UNSPEC SOCK_STREAM AI_PASSIVE IPPROTO_TCP SHUT_RD
AF_INET SOCK_DGRAM AI_CANONNAME IPPROTO_UDP SHUT_WR
AF_INET6 SOCK_RAW AI_NUMERICHOST IPPROTO_RAW SHUT_RDWR
AI_V4MAPPED
AI_ALL
AI_ADDRCONFIG
MSG_OOB MSG_PROBE MSG_WAITALL MSG_RST MSG_EOF
MSG_PEEK MSG_TRUNC MSG_FIN MSG_ERRQUEUE
MSG_DONTROUTE MSG_DONTWAIT MSG_SYN MSG_NOSIGNAL
MSG_CTRUNC MSG_EOR MSG_CONFIRM MSG_MORE