]> gitweb.factorcode.org Git - factor.git/blob - basis/io/sockets/sockets-docs.factor
Solution to Project Euler problem 65
[factor.git] / basis / io / sockets / sockets-docs.factor
1 USING: help.markup help.syntax io io.backend threads
2 strings byte-arrays continuations destructors quotations ;
3 IN: io.sockets
4
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."
7 $nl
8 "Unix domain sockets:"
9 { $subsection local }
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:"
12 { $subsection inet }
13 { $subsection <inet> }
14 "IPv4 addresses, with no host name resolution:"
15 { $subsection inet4 }
16 { $subsection <inet4> }
17 "IPv6 addresses, with no host name resolution:"
18 { $subsection inet6 }
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 } ;
22
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 } "."
31 $nl
32 "Address specifiers have the following interpretation with connection-oriented networking words:"
33 { $list
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" }
38 }
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."
40 $nl
41 "The " { $vocab-link "io.sockets.secure" } " vocabulary implements secure, encrypted sockets via SSL and TLS." ;
42
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:"
47 { $subsection send }
48 { $subsection receive }
49 "Packet-oriented sockets are closed by calling " { $link dispose } "."
50 $nl
51 "Address specifiers have the following interpretation with packet-oriented networking words:"
52 { $list
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" }
56 }
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)." ;
58
59 ARTICLE: "network-examples" "Networking examples"
60 "Send some bytes to a remote host:"
61 { $code
62     "USING: io io.encodings.ascii io.sockets strings ;"
63     "\"myhost\" 1033 <inet> ascii"
64     "[ B{ 12 17 102 } write ] with-client"
65 }
66 "Look up the IP addresses associated with a host name:"
67 { $code "USING: io.sockets ;" "\"www.apple.com\" 80 <inet> resolve-host ." } ;
68
69 ARTICLE: "network-streams" "Networking"
70 "Factor supports connection-oriented and packet-oriented communication over a variety of protocols:"
71 { $list
72     "TCP/IP and UDP/IP, over IPv4 and IPv6"
73     "Unix domain sockets (Unix only)"
74 }
75 { $subsection "network-examples" }
76 { $subsection "network-addressing" }
77 { $subsection "network-connection" }
78 { $subsection "network-packet" }
79 { $vocab-subsection "Secure sockets (SSL, TLS)" "io.sockets.secure" }
80 { $see-also "io.pipes" } ;
81
82 ABOUT: "network-streams"
83
84 HELP: local
85 { $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> } "." }
86 { $examples
87     { $code "\"/tmp/.X11-unix/0\" <local>" }
88 } ;
89
90 HELP: inet
91 { $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> } "." }
92 { $notes
93     "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."
94 }
95 { $examples
96     { $code "\"www.apple.com\" 80 <inet>" }
97 } ;
98
99 HELP: <inet>
100 { $values { "host" "a host name" } { "port" "a port number" } { "inet" inet } }
101 { $description "Creates a new " { $link inet } " address specifier." } ;
102
103 HELP: inet4
104 { $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> } "." }
105 { $notes "Most applications do not operate on IPv4 addresses directly, and instead should use the " { $link inet } " address specifier, or call " { $link resolve-host } "." }
106 { $examples
107     { $code "\"127.0.0.1\" 8080 <inet4>" }
108 } ;
109
110 HELP: <inet4>
111 { $values { "host" "an IPv4 address" } { "port" "a port number" } { "inet4" inet4 } }
112 { $description "Creates a new " { $link inet4 } " address specifier." } ;
113
114 HELP: inet6
115 { $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> } "." }
116 { $notes "Most applications do not operate on IPv6 addresses directly, and instead should use the " { $link inet } " address specifier, or call " { $link resolve-host } "." }
117 { $examples
118     { $code "\"::1\" 8080 <inet6>" }
119 } ;
120
121 HELP: <inet6>
122 { $values { "host" "an IPv6 address" } { "port" "a port number" } { "inet6" inet6 } }
123 { $description "Creates a new " { $link inet6 } " address specifier." } ;
124
125 HELP: <client>
126 { $values { "remote" "an address specifier" } { "encoding" "an encding descriptor" } { "stream" "a bidirectional stream" } { "local" "an address specifier" } }
127 { $description "Opens a network connection and outputs a bidirectional stream using the given encoding, together with the local address the socket was bound to." }
128 { $errors "Throws an error if the connection cannot be established." }
129 { $notes "The " { $link with-client } " word is easier to use in most situations." }
130 { $examples
131     { $code "\"www.apple.com\" 80 <inet> utf8 <client>" }
132 } ;
133
134 HELP: with-client
135 { $values { "remote" "an address specifier" } { "encoding" "an encoding descriptor" } { "quot" quotation } }
136 { $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." }
137 { $errors "Throws an error if the connection cannot be established." } ;
138
139 HELP: <server>
140 { $values  { "addrspec" "an address specifier" } { "encoding" "an encoding descriptor" } { "server" "a handle" } }
141 { $description
142     "Begins listening for network connections to a local address. Server objects responds to two words:"
143     { $list
144         { { $link dispose } " - stops listening on the port and frees all associated resources" }
145         { { $link accept } " - blocks until there is a connection, and returns a stream of the encoding passed to the constructor" }
146     }
147 }
148 { $notes
149     "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:"
150     { $code "f 1234 <inet> resolve-host" }
151     "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:"
152     { $code "\"localhost\" 1234 <inet> resolve-host" }
153     "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."
154     $nl
155     "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:"
156     { $unchecked-example
157         "f 0 <inet4> ascii <server>"
158         "[ addr>> . ] [ dispose ] bi"
159         "T{ inet4 f \"0.0.0.0\" 58901 }"
160     }
161 }
162 { $errors "Throws an error if the address is already in use, or if it if the system forbids access." } ;
163
164 HELP: accept
165 { $values { "server" "a handle" } { "client" "a bidirectional stream" } { "remote" "an address specifier" } }
166 { $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." }
167 { $errors "Throws an error if the server socket is closed or otherwise is unavailable." } ;
168
169 HELP: <datagram>
170 { $values { "addrspec" "an address specifier" } { "datagram" "a handle" } }
171 { $description "Creates a datagram socket bound to a local address. Datagram socket objects responds to three words:"
172     { $list
173         { { $link dispose } " - stops listening on the port and frees all associated resources" }
174         { { $link receive } " - waits for a packet" }
175         { { $link send } " - sends a packet" }
176     }
177 }
178 { $notes
179     "To accept UDP/IP packets from any host, use an address specifier returned by the following code, where 1234 is the desired port number:"
180     { $code "f 1234 <inet> resolve-host" }
181     "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:"
182     { $code "\"localhost\" 1234 <inet> resolve-host" }
183     "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."
184     "Datagrams are low-level binary ports that don't map onto streams, so the constructor does not use an encoding"
185 }
186 { $errors "Throws an error if the port is already in use, or if the OS forbids access." } ;
187
188 HELP: receive
189 { $values { "datagram" "a datagram socket" } { "packet" byte-array } { "addrspec" "an address specifier" } }
190 { $description "Waits for an incoming packet on the given datagram socket. Outputs the packet data, as well as the sender's address." }
191 { $errors "Throws an error if the packet could not be received." } ;
192
193 HELP: send
194 { $values { "packet" byte-array } { "addrspec" "an address specifier" } { "datagram" "a datagram socket" } }
195 { $description "Sends a packet to the given address." }
196 { $errors "Throws an error if the packet could not be sent." } ;
197
198 HELP: resolve-host
199 { $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } }
200 { $description "Resolves host names to IP addresses." } ;