Socket

is a simple wrapper around C sockets for Carp.

Installation

(load "https://github.com/carpentry-org/sockets@0.0.1")

Usage

Setting up a socket can be done through setup-server and setup-client or through the macros with-server and with-client.

(let [sock (Socket.setup-server "127.0.0.1" 80)]
  ; ... work with sock
  )

(let [sock (Socket.setup-client "127.0.0.1" 80)]
  ; ...
  )

(Socket.with-server sock "127.0.0.1" 80
  ; use sock as above
  )

(Socket.with-client sock "127.0.0.1" 80
  ; use sock as above
  )

After checking that they are valid using Socket.valid?, client sockets can send and read right away, whereas server sockets have to listen and accept first. Just like in C!

Alternatively, you can also use the macro with-connection in the server, like so:

(Socket.with-server server "127.0.0.1" 80
  (Socket.with-connection &server client
    (send &client "nice to meet you!")
  )
)

If you want a server that accepts connections forever, use while-connection:

(Socket.with-server server "127.0.0.1" 80
  (Socket.while-connection &server client
    (send &client "nice to meet you!")
  )
)

This will never terminate, unless interrupted by the user or failure.

accept

external

(Fn [(Ref Socket a)] Socket)

accepts new connections, returning a new socket.

buf-size

external

Int

is the size of the sockets’ read buffer.

close

external

(Fn [Socket] ())

closes a socket.

copy

external

(Fn [(Ref Socket a)] Socket)

listen

external

(Fn [(Ref Socket a)] ())

listens on a socket for new connections.

read

external

(Fn [(Ref Socket a)] String)

reads string data over a socket.

read-bytes

external

(Fn [(Ref Socket a)] (Array Byte))

reads binary data over a socket.

send

external

(Fn [(Ref Socket a), (Ref String b)] ())

sends string data over a socket.

send-bytes

external

(Fn [(Ref Socket a), (Ref (Array Byte) b)] ())

sends binary data over a socket.

setup-client

external

(Fn [(Ref String a), Int] Socket)

setup a client socket to a host using a protocol.

Example:

(Socket.setup-client-for "httpbin.org" "http" 80)

setup-client-for

external

(Fn [(Ref String a), (Ref String b), Int] Socket)

setup-server

external

(Fn [(Ref String a), Int] Socket)

setup a server socket.

valid?

external

(Fn [(Ref Socket a)] Bool)

checks whether a socket is valid.

while-connection

macro

Macro

                    (while-connection from to :rest forms)
                

create a connection socket from the server from to a socket named to, and then execute forms forever.

This functions closes the socket automatically at the end.

with-client

macro

Macro

                    (with-client name host port :rest forms)
                

create a client socket named name, connecting to host and port, and then execute forms.

This functions closes the socket automatically at the end.

with-client-for

macro

Macro

                    (with-client-for name host proto port :rest forms)
                

create a client socket named name, connecting to host and port using protocol proto, and then execute forms.

This functions closes the socket automatically at the end.

with-connection

macro

Macro

                    (with-connection from to :rest forms)
                

create a connection socket from the server from to a socket named to, and then execute forms.

This functions closes the socket automatically at the end.

with-server

macro

Macro

                    (with-server name host port :rest forms)
                

create a server socket named name, connecting to host and port, and then execute forms.

This functions closes the socket automatically at the end.