5.12 Sockets - naver/lispe GitHub Wiki

Sockets

back

(socket_create (port nblients (hostname)) create a server if hostname is omitted then the local hostname is used)
(socket_connect (hostname port) connect to the server)
(socket_wait (sock) wait for a client to connect and returns its socket id)
(socket_read (sock (socketClientId -1)) read a string on a socket. On the server side 'socketClientId' is the value returned by 'wait()'. Use 'read()' on the client side)
(socket_write (sock str (socketClientId -1)) write the string s on the socket. On the server side num is the value returned by wait()'. Use 'write(s)' on the client side)
(socket_receive (sock nb (socketClientId -1)) read a string on a socket in a raw environment. On the server side 'socketClientId' is the value returned by 'wait()'. Use 'receive()' on the client side)
(socket_get (sock (socketClientId -1)) get one character from a socket in a non Tamgu environment. On the server side 'socketClientId' is the value returned by 'wait()'. Use 'get()' on the client side)
(socket_send (sock str (socketClientId -1)) write the string s on the socket in a non Tamgu environment. On the server side num is the value returned by wait()'. Use 'send(string s)' on the client side)
(socket_close (sock (socketClientId -1)) Close a socket. On the server side if 'socketClientId' is provided (it is the value returned by wait()) it closes the client socket otherwise it closes the current socket.)
(socket_blocking (sock flag) if 'flag' is true the socket works in 'blocking' mode otherwise in 'non blocking' mode)
(socket_settimeout (sock tm) Set a time out of 't' seconds on the socket)
(socket_gethostname (sock) return the current host name)
(socket_port (sock) return the current port number)
(socket_getpeername (sock socketClientId) return the current peer name)

Example of a server

; Creation of a server
(setq server (socket_create 2654 10))

(println (socket_gethostname server))

(println 'Waiting 'for 'a 'connection)
; We wait for a connection
(setq idclient (socket_wait server))


; We then read on this idclient
(println 'Reading (socket_read server idclient))

Example of a client

; we create a connection
; hostname is the name of the machine to which to connect
; as given by 'socket_gethostname' above

(setq client (socket_connect "hostname" 2654))

; We then write something on the socket
(socket_write client "This is a test")