Socket

Mosh provides a simple Socket library.

This library is almost compatible with Ypsilon’s (socket) library.  Thanks to Yoshikatsu Fujita.

Example

;; 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")
Summary
SocketMosh provides a simple Socket library.
(mosh socket)Socket Library
Functions
socket?Returns #t when obj is socket.
make-client-socketReturns a client socket connected to an Internet address.
make-server-socketReturns a server socket waiting for connections.
socket-recvReceives a binary data block from a socket.
socket-recv!Receives a binary data block from a socket.
socket-sendSends a binary data block to a socket.
socket-closeCloses a socket.
socket-shutdownShutdowns a socket.
socket-acceptWait for an incoming connection request, and returns a fresh connected client socket.
socket-portReturns a fresh binary input/output port associated with a socket.
call-with-socketCalls 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

(mosh socket)

Socket Library

Summary
Functions
socket?Returns #t when obj is socket.
make-client-socketReturns a client socket connected to an Internet address.
make-server-socketReturns a server socket waiting for connections.
socket-recvReceives a binary data block from a socket.
socket-recv!Receives a binary data block from a socket.
socket-sendSends a binary data block to a socket.
socket-closeCloses a socket.
socket-shutdownShutdowns a socket.
socket-acceptWait for an incoming connection request, and returns a fresh connected client socket.
socket-portReturns a fresh binary input/output port associated with a socket.
call-with-socketCalls 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

Functions

socket?

Returns #t when obj is socket.

Prototype

(socket? obj)

Returns

#t when obj is socket.

make-client-socket

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

Prototype

(make-client-socket node service . options)

Parameters

nodea network address.  (examples: “www.w3.org”, “localhost”, “128.30.52.45”)
servicea 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-flagsan additional options specifier.  AI_ADDRCONFIG, AI_ALL, AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV, AI_PASSIVE, AI_V4MAPPED or 0(default).
ai-protocola protocol specifier.  IPPROTO_TCP, IPPROTO_UDP, IPPROTO_RAW or 0(default).

Returns

client socket.

make-server-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

Prototype

(make-server-socket service . options)

Parameters

servicea 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).

Returns

server socket.

socket-recv

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.

Prototype

(socket-recv socket size . flags)

Parameters

socketsocket.
sizesize to receive.
flags(optional)flags to recv(2).  Default values is 0.

Returns

binary data as bytevector

socket-recv!

Receives a binary data block from a socket.

Prototype

(socket-recv! socket bytevector start size flags)

Parameters

socketsocket.
bytevectorbuffer to store received data.
startindex of buffer to written.
sizesize to receive.
flagsflags to recv(2).

Returns

data size

socket-send

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.

Prototype

(socket-send socket bytevector . flags)

Parameters

socketsocket.
bytevectordata to send.
flags(optional)flags to send(2).  Default values is 0.

Returns

sent data size.

socket-close

Closes a socket.

On Windows, shutdown is called internally.

Prototype

(socket-close socket)

socket-shutdown

Shutdowns a socket.

Prototype

(socket-shutdown socket how)

Parameters

socketsocket.
howSHUT_RD, SHUT_WR, or SHUT_RDWR.

socket-accept

Wait for an incoming connection request, and returns a fresh connected client socket.

Prototype

(accept socket)

Returns

client socket.

socket-port

Returns a fresh binary input/output port associated with a socket.

Prototype

(socket-port socket)

Returns

binary input/output port.

call-with-socket

Calls a procedure with a socket as an argument.  This procedure has an analogy to call-with-port of (rnrs io ports).

Prototype

(call-with-socket socket proc)

Returns

Returned value(s) of proc.

Constants

AF_INET

ai-family

AF_INET6

ai-family

AF_UNSPEC

ai-family

SOCK_STREAM

ai-socktype

SOCK_DGRAM

ai-socktype

AI_ADDRCONFIG

ai-flags

AI_ALL

ai-flags

AI_CANONNAME

ai-flags

AI_NUMERICHOST

ai-flags

AI_NUMERICSERV

ai-flags

AI_PASSIVE

ai-flags

AI_V4MAPPED

ai-flags

IPPROTO_TCP

ai-protocol

IPPROTO_UDP

ai-protocol

IPPROTO_RAW

ai-protocol

SHUT_RD

SHUT_WR

SHUT_RDWR

Close