Next: , Previous: , Up: posix socket   [Index]


4.15.6 Network sockets operations

Function: socket namespace style protocol

Interface to the C function socket(), (libc)socket. Create a new socket descriptor and return it as non–negative fixnum; if an error occurs raise an exception. All the arguments must be fixnums.

Function: shutdown sock how

Interface to the C function shutdown(), (libc)shutdown. Close the socket sock according to how, which can be one of the constants SHUT_RD, SHUT_WR or SHUT_RDWR. If successful return unspecified values, else raise an exception.

Function: socketpair namespace style protocol

Interface to the C function socketpair(), (libc)socketpair. Create a pairs of connected sockets and return two values being the descriptors as non–negative fixnums; if an error occurs raise an exception. All the arguments must be fixnums; namespace must be AF_LOCAL.

(import (rnrs)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(let-values (((a b) (px.socketpair AF_LOCAL SOCK_DGRAM 0)))
  (px.write a '#vu8(1 2 3 4) 4)
  (let ((buf (make-bytevector 4)))
    (px.read b buf 4)
    (px.shutdown a SHUT_RDWR)
    (px.shutdown b SHUT_RDWR)
    buf))
⇒ #vu8(1 2 3 4)
Function: connect sock addr

Interface to the C function connect(), (libc)connect. Connect the socket sock to the address specified by the bytevector addr which must hold a concrete instance of struct sockaddr. If successful return unspecified values, else raise an exception.

Function: listen sock pending_conns

Interface to the C function listen(), (libc)listen. Enable the socket sock to accept connections; the fixnum pending_conns specifies the maximum length of the pending connection requests queue, it can be at most equal to the platform constant SOMAXCONN. If successful return unspecified values, else raise an exception.

Function: accept sock

Interface to the C function accept(), (libc)accept. Accept an incoming connection to the server socket sock.

If successful and a connection is accepted return two values: a non–negative fixnum representing the socket, a bytevector representing the client address as struct sockaddr.

If successful and no connection is accepted return two values both being false; this is the case of accept() returning EWOULDBLOCK which means that sock is in non–blocking mode and no pending connections exist.

If an error occurs raise an exception.

Function: bind sock sockaddr

Interface to the C function bind(), (libc)bind. Bind the socket descriptor sock to the address specified by the struct sockaddr in the bytevector sockaddr. If successful return unspecified values, else raise an exception.

Function: getpeername sock

Interface to the C function getpeername(), (libc)getpeername. Retrieve informations about the address of the socket to which the sock socket is connected to. If successful return a bytevector holding a struct sockaddr, else raise an exception.

Function: getsockname sock

Interface to the C function getsockanme(), (libc)getsockname. Retrieve informations about the address bound to the socket descriptor sock. If successful return a bytevector holding the struct sockaddr, else raise an exception.

Function: send sock buffer size flags

Interface to the C function send(), (libc)send. Like write() but with the additional argument flags which must be a fixnum: write data from buffer to the socket sock. If successful return a non–negative fixnum representing the number of bytes actually sent, else raise an exception.

buffer and size must represent a generalised C string, (vicare-libs)Introduction to generalised C strings. When buffer is a Scheme string: it is converted to a bytevector with string->ascii.

Function: recv sock buffer size flags

Interface to the C function recv(), (libc)recv. Like read() but with the additional argument flags which must be a fixnum: read data from sock and store it in buffer. If successful return a non–negative fixnum representing the number of bytes actually received, else raise an exception.

buffer and size must represent a generalised C buffer, (vicare-libs)Introduction to generalised C buffers.

Function: sendto sock buffer size flags addr

Interface to the C function sendto(), (libc)sendto. Like send() but with the additional argument addr which must be a bytevector holding a struct sockaddr: write data from buffer to the socket sock to the destination specified by addr.

buffer and size must represent a generalised C string, (vicare-libs)Introduction to generalised C strings. When buffer is a Scheme string: it is converted to a bytevector with string->ascii.

If successful return a non–negative fixnum representing the number of bytes actually sent, else raise an exception.

Function: recvfrom sock buffer size flags

Interface to the C function recvfrom(), (libc)recvfrom. Like recv() but additionally retrieve informations about the address of the sender: read data from sock and store it in buffer.

buffer and size must represent a generalised C buffer, (vicare-libs)Introduction to generalised C buffers.

If successful return two values: a non–negative fixnum representing the number of bytes actually received, a bytevector holding a struct sockaddr representing the address of the sender; else raise an exception.

The functions getsockopt and setsockopt, and their variants, can be used to configure a number of socket options. To inspect the list of available options read the following manual pages (on GNU+Linux) systems:

Function: getsockopt sock level option optval

Interface to the C function getsockopt(), (libc)getsockopt. Retrieve the value of option of socket sock at level and store it in the bytevector optval. option and level must be fixnums. If successful return unspecified values, else raise an exception.

Function: setsockopt sock level option optval

Interface to the C function setsockopt(), (libc)setsockopt. Set a new value for option of socket sock at level reading it from the bytevector optval. option and level must be fixnums. If successful return unspecified values, else raise an exception.

Function: getsockopt/int sock level option
Function: setsockopt/int sock level option optval

Like getsockopt and setsockopt but specially handle the case of an option with value represented by a C language int. optval must appropriately be a boolean or an exact integer representable as a platform’s C language int.

Function: getsockopt/size_t sock level option
Function: setsockopt/size_t sock level option optval

Like getsockopt and setsockopt but specially handle the case of an option with value represented by a C language size_t. optval must be an exact integer representable as a platform’s C language size_t.

Function: setsockopt/linger sock onoff linger

Similar to setsockopt, but set the SO_LINGER option by directly specifying the values for the fields of the struct linger type. (libc)struct linger.

onoff must be a boolean. linger must be a fixnum.

Function: getsockopt/linger sock

Similar to getsockopt, but inspect directly the option SO_LINGER by returning the current values of the fields of the struct linger type. (libc)struct linger.

Return 2 values: a boolean representing the onoff field, an exact integer representing the linger field.

Function: tcp-connect hostname service
Function: tcp-connect hostname service log-proc

Establish a client connection to the remote host identified by the string hostname, connecting to the port associated to the string or number service.

If successful return a textual input/output port associated to the socket file descriptor, configured with line buffer mode, UTF-8 transcoder, and none end–of–line translation. Closing the returned port will close the connection.

This function makes use of getaddrinfo to obtain possible network interfaces to connect to and attempts to connect to all of them stopping at the first success.

The optional argument log-proc must be a function and it is called while attempting to connect to an interface; the arguments are:

(log-proc action hostname service sockaddr)

where: action is one of the symbols attempt, success, fail; sockaddr is a bytevector holding an instance of struct sockaddr from a struct addrinfo.

To actually attempt a connection: make use of the function referenced by the parameter tcp-connect.connect-proc, which is initialised to connect.

The following example will download the main page from http://google.it:

#!r6rs
(import (except (vicare)
                log)
  (prefix (vicare posix) px.))

(define (log template . args)
  (apply fprintf (current-error-port)
         template args))

(define (send line port)
  (log "sending: ~s\n" line)
  (display line port))

(define (recv in-port)
  (let-values (((str-port getter) (open-string-output-port)))
    (let next ((line (read-line in-port)))
      (if (or (eof-object? line)
              (string=? line "\r")
              (string=? line ".\r"))
          (getter)
        (begin
          (log "received: ~s\n" line)
          (display line str-port)
          (next (read-line in-port)))))))

(define p
  (px.tcp-connect "google.it" "http"))

(send "GET / HTTP/1.0\r\n\r\n" p)
(recv p)
(recv p)

(close-port p)
Function: tcp-connect/binary hostname service
Function: tcp-connect/binary hostname service log-proc

Establish a client connection to the remote host identified by the string hostname, connecting to the port associated to the string or number service. This function is similar to tcp-connect, but it returns a binary input output port.

Parameter: tcp-connect.connect-proc

References a function used by tcp-connect to actually attempt a TCP connection. It is initialised to connect.

This parameter allows us to fake a failed connection attempt by registering a function that raises an &errno exception with errno code among:

EADDRNOTAVAIL   ETIMEDOUT
ECONNREFUSED    ENETUNREACH

Next: , Previous: , Up: posix socket   [Index]