Mosh provides a simple Socket library.
This library is almost compatible with Ypsilon’s (socket) library. Thanks to Yoshikatsu Fujita.
;; Sample of IRC client bot (import (rnrs) (mosh) (mosh socket)) (define (irc-bot server port nick channel) (let ([socket (make-client-socket server port)]) (define (send text) (assert (<= (string-length text) 510)) (socket-send socket (string->utf8 (string-append text "\r\n")))) (define (recv) (utf8->string (socket-recv socket 512))) (define (say text) (send (format "PRIVMSG ~a :~a" channel text))) (send (format "NICK ~a" nick)) (send (format "USER ~a 0 * :~a" nick nick)) (send (format "JOIN ~a" channel)) (let loop ([data (recv)]) (cond [(#/:([^!]+).*PRIVMSG[^:]+:(.*)/ data) => (lambda (m) (format #t "<~a> ~a\n" (m 1) (m 2)) (say (format "what is ~s?" (m 2))))] [(#/^PING/ data) (send "PONG 0")] [(#/:.*433.*Nickname is already in use.*/ data) (error 'irc "Nickname is already in use")]) (loop (recv))) (socket-close socket))) (irc-bot "irc.freenode.net" "6666" "higepon" "#mosh")
Socket | Mosh provides a simple Socket library. |
(mosh socket) | Socket Library |
Functions | |
socket? | Returns #t when obj is socket. |
make-client-socket | Returns a client socket connected to an Internet address. |
make-server-socket | Returns a server socket waiting for connections. |
socket-recv | Receives a binary data block from a socket. |
socket-recv! | Receives a binary data block from a socket. |
socket-send | Sends a binary data block to a socket. |
socket-close | Closes a socket. |
socket-shutdown | Shutdowns a socket. |
socket-accept | Wait for an incoming connection request, and returns a fresh connected client socket. |
socket-port | Returns a fresh binary input/output port associated with a socket. |
call-with-socket | Calls a procedure with a socket as an argument. |
Constants | |
AF_INET | |
AF_INET6 | |
AF_UNSPEC | |
SOCK_STREAM | |
SOCK_DGRAM | |
AI_ADDRCONFIG | |
AI_ALL | |
AI_CANONNAME | |
AI_NUMERICHOST | |
AI_NUMERICSERV | |
AI_PASSIVE | |
AI_V4MAPPED | |
IPPROTO_TCP | |
IPPROTO_UDP | |
IPPROTO_RAW | |
SHUT_RD | |
SHUT_WR | |
SHUT_RDWR |
Socket Library
Functions | |
socket? | Returns #t when obj is socket. |
make-client-socket | Returns a client socket connected to an Internet address. |
make-server-socket | Returns a server socket waiting for connections. |
socket-recv | Receives a binary data block from a socket. |
socket-recv! | Receives a binary data block from a socket. |
socket-send | Sends a binary data block to a socket. |
socket-close | Closes a socket. |
socket-shutdown | Shutdowns a socket. |
socket-accept | Wait for an incoming connection request, and returns a fresh connected client socket. |
socket-port | Returns a fresh binary input/output port associated with a socket. |
call-with-socket | Calls a procedure with a socket as an argument. |
Constants | |
AF_INET | |
AF_INET6 | |
AF_UNSPEC | |
SOCK_STREAM | |
SOCK_DGRAM | |
AI_ADDRCONFIG | |
AI_ALL | |
AI_CANONNAME | |
AI_NUMERICHOST | |
AI_NUMERICSERV | |
AI_PASSIVE | |
AI_V4MAPPED | |
IPPROTO_TCP | |
IPPROTO_UDP | |
IPPROTO_RAW | |
SHUT_RD | |
SHUT_WR | |
SHUT_RDWR |
Returns a client socket connected to an Internet address.
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-client-socket node service . options)
node | a network address. (examples: “www.w3.org”, “localhost”, “128.30.52.45”) |
service | a network service. (examples: “http”, “ssh”, “80”, “22”) |
ai-family(optional) | an address family specifier. AF_INET (default), AF_INET6 or AF_UNSPEC. |
ai-socktype(optional) | a socket type specifier. SOCK_STREAM (default) or SOCK_DGRAM. |
ai-flags | an additional options specifier. AI_ADDRCONFIG, AI_ALL, AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV, AI_PASSIVE, AI_V4MAPPED or 0(default). |
ai-protocol | a protocol specifier. IPPROTO_TCP, IPPROTO_UDP, IPPROTO_RAW or 0(default). |
client socket.
Returns a server socket waiting for connections.
The arguments service, ai-family, ai-socktype, and ai-protocol will be passed to getaddrinfo(3) as a correspondent parameter to setup server socket.
Refer to getaddrinfo(3) manual page for details
(make-server-socket service . options)
service | a network service. (examples: “http”, “ssh”, “80”, “22”) |
ai-family(optional) | an address family specifier. AF_INET (default), AF_INET6 or AF_UNSPEC. |
ai-socktype(optional) | a socket type specifier. SOCK_STREAM (default) or SOCK_DGRAM. |
ai-protocol(optional) | a protocol specifier. IPPROTO_TCP, IPPROTO_UDP, IPPROTO_RAW or 0(default). |
server socket.
Receives a binary data block from a socket.
Socket-recv uses 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-recv socket size . flags)
socket | socket. |
size | size to receive. |
flags(optional) | flags to recv(2). Default values is 0. |
binary data as bytevector
Sends a binary data block to a socket.
socket-send uses 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-send socket bytevector . flags)
socket | socket. |
bytevector | data to send. |
flags(optional) | flags to send(2). Default values is 0. |
sent data size.