retroforth/interface/sockets.retro
crc 17dfce7e79 retro-unix: work on adding comments to the socket interface (forth part)
FossilOrigin-Name: 4f1a44ca993c6b9e348da2fc4cf5b9cf59c7212d7667efa3fb1febe65fa01d8f
2020-09-16 19:28:54 +00:00

86 lines
2.1 KiB
Text

# Sockets
This implements the Forth part of the socket I/O interface.
As with the file I/O device, the socket words are a thin
wrapper over the standard Unix socket functions. This means
that it is fairly low level.
~~~
{{
'Sockets var
:identify
@Sockets n:zero? 0; drop
#7 io:scan-for dup n:negative?
[ drop 'IO_DEVICE_TYPE_0004_NOT_FOUND s:put nl ]
[ !Sockets ] choose ;
---reveal---
:socket:operation identify @Sockets io:invoke ;
}}
~~~
`socket:gethostbyname` gets the host IP in dottode notation. Provide
an address to hold the dotted notation and a string containing the
host name.
~~~
:socket:gethostbyname (as-) #0 socket:operation ;
~~~
`socket:create` creates a new socket. It does not take any
parameters, and returns the socket id.
~~~
:socket:create (-n) #1 socket:operation ;
~~~
`socket:bind` binds a socket to a port. The socket should be
provided as the socket id, and the port number should be a
string. This will return 0 if successful, -1 if not successful,
and a host specific error code.
~~~
:socket:bind (sn-n) #2 socket:operation ;
~~~
~~~
:socket:listen (nn-nn) #3 socket:operation ;
~~~
~~~
:socket:accept (n-nn) #4 socket:operation ;
~~~
~~~
:socket:connect (n-nn) #5 socket:operation ;
~~~
`socket:connect` sends a string to a socket. This will return
the number of characters sent and an error code. It takes a
string and a socket id.
~~~
:socket:send (sn-nn) #6 socket:operation ;
~~~
`socket:recv` is a wrapper over recv(2). It takes an address,
a maxmimum number of bytes to read, and a socket id. It returns
the number of bytes received and an error code.
~~~
:socket:recv (ann-nn) #8 socket:operation ;
~~~
`socket:close` is a wrapper over close(2). It takes a socket id
and closes the socket.
~~~
:socket:close (n-) #10 socket:operation ;
~~~
`socket:configure` is used before `socket:bind` to set some
internal state. Takes the host name and port, both as strings.
Does not return anything.
~~~
:socket:configure (ss-) #11 socket:operation ;
~~~