1 USING: help.markup help.syntax io io.backend threads
2 strings byte-arrays continuations destructors quotations ;
5 ARTICLE: "network-addressing" "Address specifiers"
6 "The networking words are quite general and work with " { $emphasis "address specifiers" } " rather than concrete concepts such as host names. There are four types of address specifiers."
10 { $subsection <local> }
11 "Internet host name/port number pairs; the host name is resolved to an IPv4 or IPv6 address using the operating system's resolver:"
13 { $subsection <inet> }
14 "IPv4 addresses, with no host name resolution:"
16 { $subsection <inet4> }
17 "IPv6 addresses, with no host name resolution:"
19 { $subsection <inet6> }
20 "While the " { $link inet } " addressing specifier is capable of performing name lookups when passed to " { $link <client> } ", sometimes it is necessary to look up a host name without making a connection:"
21 { $subsection resolve-host } ;
23 ARTICLE: "network-connection" "Connection-oriented networking"
24 "Network connections can be established with this word:"
25 { $subsection <client> }
26 { $subsection with-client }
27 "Connection-oriented network servers are implemented by first opening a server socket, then waiting for connections:"
28 { $subsection <server> }
29 { $subsection accept }
30 "Server sockets are closed by calling " { $link dispose } "."
32 "Address specifiers have the following interpretation with connection-oriented networking words:"
34 { { $link local } " - Unix domain stream sockets on Unix systems" }
35 { { $link inet } " - a TCP/IP connection to a host name/port number pair which can resolve to an IPv4 or IPv6 address" }
36 { { $link inet4 } " - a TCP/IP connection to an IPv4 address and port number; no name lookup is performed" }
37 { { $link inet6 } " - a TCP/IP connection to an IPv6 address and port number; no name lookup is performed" }
39 "The " { $vocab-link "io.servers.connection" } " library defines high-level wrappers around " { $link <server> } " which makes it easy to listen for IPv4, IPv6 and secure socket connections simultaneously, perform logging, and optionally only allow connections from the loopback interface."
41 "The " { $vocab-link "io.sockets.secure" } " vocabulary implements secure, encrypted sockets via SSL and TLS." ;
43 ARTICLE: "network-packet" "Packet-oriented networking"
44 "A packet-oriented socket can be opened with this word:"
45 { $subsection <datagram> }
46 "Packets can be sent and received with a pair of words:"
48 { $subsection receive }
49 "Packet-oriented sockets are closed by calling " { $link dispose } "."
51 "Address specifiers have the following interpretation with packet-oriented networking words:"
53 { { $link local } " - Unix domain datagram sockets on Unix systems" }
54 { { $link inet4 } " - a TCP/IP connection to an IPv4 address and port number; no name lookup is performed" }
55 { { $link inet6 } " - a TCP/IP connection to an IPv6 address and port number; no name lookup is performed" }
57 "The " { $link inet } " address specifier is not supported by the " { $link send } " word because a single host name can resolve to any number of IPv4 or IPv6 addresses, therefore there is no way to know which address should be used. Applications should call " { $link resolve-host } " then use some kind of strategy to pick the correct address (for example, by sending a packet to each one and waiting for a response, or always assuming IPv4)." ;
59 ARTICLE: "network-streams" "Networking"
60 "Factor supports connection-oriented and packet-oriented communication over a variety of protocols:"
62 "TCP/IP and UDP/IP, over IPv4 and IPv6"
63 "Unix domain sockets (Unix only)"
65 { $subsection "network-addressing" }
66 { $subsection "network-connection" }
67 { $subsection "network-packet" }
68 { $vocab-subsection "Secure sockets (SSL, TLS)" "io.sockets.secure" }
69 { $see-also "io.pipes" } ;
71 ABOUT: "network-streams"
74 { $class-description "Local address specifier for Unix domain sockets on Unix systems. The " { $snippet "path" } " slot holds the path name of the socket. New instances are created by calling " { $link <local> } "." }
76 { $code "\"/tmp/.X11-unix/0\" <local>" }
80 { $class-description "Host name/port number specifier for TCP/IP and UDP/IP connections. The " { $snippet "host" } " and " { $snippet "port" } " slots hold the host name and port name or number, respectively. New instances are created by calling " { $link <inet> } "." }
82 "This address specifier is only supported by " { $link <client> } ", which calls " { $link resolve-host } " to obtain a list of IP addresses associated with the host name, and attempts a connection to each one in turn until one succeeds. Other network words do not accept this address specifier, and " { $link resolve-host } " must be called directly; it is then up to the application to pick the correct address from the (possibly several) addresses associated to the host name."
85 { $code "\"www.apple.com\" 80 <inet>" }
89 { $values { "host" "a host name" } { "port" "a port number" } { "inet" inet } }
90 { $description "Creates a new " { $link inet } " address specifier." } ;
93 { $class-description "IPv4 address/port number specifier for TCP/IP and UDP/IP connections. The " { $snippet "host" } " and " { $snippet "port" } " slots hold the IPv4 address and port number, respectively. New instances are created by calling " { $link <inet4> } "." }
94 { $notes "Most applications do not operate on IPv4 addresses directly, and instead should use the " { $link inet } " address specifier, or call " { $link resolve-host } "." }
96 { $code "\"127.0.0.1\" 8080 <inet4>" }
100 { $values { "host" "an IPv4 address" } { "port" "a port number" } { "inet4" inet4 } }
101 { $description "Creates a new " { $link inet4 } " address specifier." } ;
104 { $class-description "IPv6 address/port number specifier for TCP/IP and UDP/IP connections. The " { $snippet "host" } " and " { $snippet "port" } " slots hold the IPv6 address and port number, respectively. New instances are created by calling " { $link <inet6> } "." }
105 { $notes "Most applications do not operate on IPv6 addresses directly, and instead should use the " { $link inet } " address specifier, or call " { $link resolve-host } "." }
107 { $code "\"::1\" 8080 <inet6>" }
111 { $values { "host" "an IPv6 address" } { "port" "a port number" } { "inet6" inet6 } }
112 { $description "Creates a new " { $link inet6 } " address specifier." } ;
115 { $values { "remote" "an address specifier" } { "encoding" "an encding descriptor" } { "stream" "a bidirectional stream" } { "local" "an address specifier" } }
116 { $description "Opens a network connection and outputs a bidirectional stream using the given encoding, together with the local address the socket was bound to." }
117 { $errors "Throws an error if the connection cannot be established." }
118 { $notes "The " { $link with-client } " word is easier to use in most situations." }
120 { $code "\"www.apple.com\" 80 <inet> utf8 <client>" }
124 { $values { "remote" "an address specifier" } { "encoding" "an encoding descriptor" } { "quot" quotation } }
125 { $description "Opens a network connection and calls the quotation in a new dynamic scope with " { $link input-stream } " and " { $link output-stream } " rebound to the network streams. The local address the socket is connected to is stored in the " { $link local-address } " variable, and the remote address is stored in the " { $link remote-address } " variable." }
126 { $errors "Throws an error if the connection cannot be established." } ;
129 { $values { "addrspec" "an address specifier" } { "encoding" "an encoding descriptor" } { "server" "a handle" } }
131 "Begins listening for network connections to a local address. Server objects responds to two words:"
133 { { $link dispose } " - stops listening on the port and frees all associated resources" }
134 { { $link accept } " - blocks until there is a connection, and returns a stream of the encoding passed to the constructor" }
138 "To start a TCP/IP server which listens for connections from any host, use an address specifier returned by the following code, where 1234 is the desired port number:"
139 { $code "f 1234 <inet> resolve-host" }
140 "To start a server which listens for connections from the loopback interface only, use an address specifier returned by the following code, where 1234 is the desired port number:"
141 { $code "\"localhost\" 1234 <inet> resolve-host" }
142 "Since " { $link resolve-host } " can return multiple address specifiers, your server code must listen on them all to work properly. The " { $vocab-link "io.servers.connection" } " vocabulary can be used to help with this."
144 "To start a TCP/IP server which listens for connections on a randomly-assigned port, set the port number in the address specifier to 0, and then read the " { $snippet "addr" } " slot of the server instance to obtain the actual port number it is listening on:"
146 "f 0 <inet4> ascii <server>"
147 "[ addr>> . ] [ dispose ] bi"
148 "T{ inet4 f \"0.0.0.0\" 58901 }"
151 { $errors "Throws an error if the address is already in use, or if it if the system forbids access." } ;
154 { $values { "server" "a handle" } { "client" "a bidirectional stream" } { "remote" "an address specifier" } }
155 { $description "Waits for a connection to a server socket created by " { $link <server> } ", and outputs a bidirectional stream when the connection has been established. The encoding of this stream is the one that was passed to the server constructor." }
156 { $errors "Throws an error if the server socket is closed or otherwise is unavailable." } ;
159 { $values { "addrspec" "an address specifier" } { "datagram" "a handle" } }
160 { $description "Creates a datagram socket bound to a local address. Datagram socket objects responds to three words:"
162 { { $link dispose } " - stops listening on the port and frees all associated resources" }
163 { { $link receive } " - waits for a packet" }
164 { { $link send } " - sends a packet" }
168 "To accept UDP/IP packets from any host, use an address specifier returned by the following code, where 1234 is the desired port number:"
169 { $code "f 1234 <inet> resolve-host" }
170 "To accept UDP/IP packets from the loopback interface only, use an address specifier returned by the following code, where 1234 is the desired port number:"
171 { $code "\"localhost\" 1234 <inet> resolve-host" }
172 "Since " { $link resolve-host } " can return multiple address specifiers, your code must create a datagram socket for each one and co-ordinate packet sending accordingly."
173 "Datagrams are low-level binary ports that don't map onto streams, so the constructor does not use an encoding"
175 { $errors "Throws an error if the port is already in use, or if the OS forbids access." } ;
178 { $values { "datagram" "a datagram socket" } { "packet" byte-array } { "addrspec" "an address specifier" } }
179 { $description "Waits for an incoming packet on the given datagram socket. Outputs the packet data, as well as the sender's address." }
180 { $errors "Throws an error if the packet could not be received." } ;
183 { $values { "packet" byte-array } { "addrspec" "an address specifier" } { "datagram" "a datagram socket" } }
184 { $description "Sends a packet to the given address." }
185 { $errors "Throws an error if the packet could not be sent." } ;
188 { $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } }
189 { $description "Resolves host names to IP addresses." } ;