2020-09-16 21:07:06 +02:00
|
|
|
# Sockets
|
2019-06-06 19:15:52 +02:00
|
|
|
|
2020-09-16 21:07:06 +02:00
|
|
|
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.
|
2019-06-06 19:15:52 +02:00
|
|
|
|
2019-06-05 22:33:09 +02:00
|
|
|
~~~
|
2021-04-30 17:09:01 +02:00
|
|
|
:socket:operation
|
|
|
|
#7 io:scan-for
|
2021-08-04 19:15:20 +02:00
|
|
|
dup n:negative? [ drop 'Error:_device_(0007)_not_found s:put nl
|
|
|
|
'Try_rebuilding_with: s:put nl
|
|
|
|
'make_OPTIONS="-DENABLE\_SOCKETS" s:format
|
|
|
|
s:put nl ] if;
|
2021-04-30 17:09:01 +02:00
|
|
|
io:invoke ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`socket:gethostbyname` gets the host IP in dottode notation. Provide
|
|
|
|
an address to hold the dotted notation and a string containing the
|
|
|
|
host name.
|
2019-06-05 22:33:09 +02:00
|
|
|
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:gethostbyname (as-) #0 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`socket:create` creates a new socket. It does not take any
|
|
|
|
parameters, and returns the socket id.
|
|
|
|
|
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:create (-n) #1 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`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.
|
|
|
|
|
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:bind (sn-n) #2 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
2020-09-16 21:42:55 +02:00
|
|
|
`socket:listen` prepares a socket for accepting incoming
|
|
|
|
connections. Takes a backlog count and a socket id. Returns
|
|
|
|
a flag (0 success, -1 failed) and an error code.
|
|
|
|
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:listen (nn-nn) #3 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
2020-09-16 21:42:55 +02:00
|
|
|
`socket:accept` begins accepting connections on the provided
|
|
|
|
socket id. Returns a new socket id and an error code.
|
|
|
|
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:accept (n-nn) #4 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
2020-09-16 21:42:55 +02:00
|
|
|
`socket:connect` connects to a server. Provide a socket id,
|
|
|
|
this will return a status flag and an error code.
|
|
|
|
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:connect (n-nn) #5 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
2020-09-16 21:42:55 +02:00
|
|
|
`socket:send` sends a string to a socket. This will return
|
2020-09-16 21:28:54 +02:00
|
|
|
the number of characters sent and an error code. It takes a
|
|
|
|
string and a socket id.
|
|
|
|
|
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:send (sn-nn) #6 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`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.
|
|
|
|
|
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:recv (ann-nn) #8 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`socket:close` is a wrapper over close(2). It takes a socket id
|
|
|
|
and closes the socket.
|
|
|
|
|
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:close (n-) #10 socket:operation ;
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`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.
|
2021-05-28 20:58:21 +02:00
|
|
|
|
2020-09-16 21:28:54 +02:00
|
|
|
~~~
|
2020-09-16 21:07:06 +02:00
|
|
|
:socket:configure (ss-) #11 socket:operation ;
|
2019-06-05 22:55:35 +02:00
|
|
|
~~~
|