TCP/IP Package Support

The loadable file tcpip.fth adds support for the TCP/IP package provided with the ZSock application, and allows you to create full TCP/IP applications in Forth.

IP Address Conversion

ip> ( Dip-addr -- a b c d )
Converts the IP address held in the double number on stack into the four individual numbers representing the dotted quad.
>ip ( a b c d -- Dip-addr )
Converts four numbers representing a dotted quad into a double-number IP address.

Protocol Constants

tcp ( -- prot )
Gives the protocol number for the TCP protocol.
udp ( -- prot )
Gives the protocol number for the UDP protocol.

Socket Calls

GoTCP ( -- )
Must be called to allow TCP/IP to do some work every so often.
sock_write ( sck caddr u1 -- u2 )
Writes u1 bytes from caddr to the specified socket, returning u2 the number of bytes actually written.
sock_emit ( c sck -- u )
Emits a character to the socket, returning u the number of bytes actually written.
sock_flush ( sck -- )
Flushes any outstanding writes to the socket.
sock_read ( sck caddr u1 -- u2 )
Reads up to u1 bytes to caddr from the specified socket, returning u2 the number of bytes actually read.
sock_close ( sck -- )
Closes the socket.
sock_abort ( sck -- )
Aborts the socket.
sock_shutdown ( sck -- >
Shuts down the socket.
sock_data? ( sck -- u )
Returns the number of bytes of data available for reading from the socket.
sock_opened? ( sck -- 0 | 1 )
Tests whether the socket is opened.
sock_closed? ( sck -- 0 | 1 )
Tests whether the socked is closed.
sock_listen ( Dip port 0 prot -- sck|0 )
Listens on port for a connection from Dip, returning socket or 0.
sock_open ( Dip port 0 prot -- sck|0 )
Actively opens a connection on port to Dip, returining socket or 0.
sock_settimeout ( sck n -- )
Sets the socket timeout.
sock_gettimeout ( sck -- n )
Gets the socket timeout.
sock_waitopen ( sck -- 0|1 )
Waits for the socket to enter an opened state.
sock_waitclose ( sck -- 0|1 )
Waits for the socket to enter a closed state.

Name Resolution

resolve ( 0-addr -- Dip | D0 )
Resolves null-terminated name to IP address.
-resolve ( Dip buffer -- flag )
Attempts to reverse-resolve IP address to name, placing it in buffer and returning true if successful.
hostaddr ( -- Dip )
Returns host IP address.
domain ( buffer -- )
Places host domain name into buffer.
0>ip ( 0-addr -- Dip | D0 )
Converts null-terminated dotted quad to IP address.
ip>0 ( Dip buffer -- )
Converts IP address to null-terminated dotted quad in buffer.

Service & Protocol Lookups

service>port ( 0-addr -- u )
Returns port number for service name.
port>service ( u buffer -- )
Returns service name for port number.
port>proto ( u -- n )
Returns protocol on port number.
service>proto ( 0-addr -- n )
Returns protocol for service name.
name>proto ( 0-addr -- n )
Returns protocol number for name.
proto>name ( n buffer -- )
Returns protocol name for number.

Other services

The TCP/IP package provides many more calls than are supported here. The remaining ones are mostly concerned with the internal workings of TCP/IP (which should be left to the ZSock application) or with daemons, or are not particularly useful to Forth applications. However, you may add them yourself with the PKGCALL defining word if required.

CamelForth Package Handling

More about CamelForth

Back to the Z88 home page