]> gitweb.factorcode.org Git - factor.git/blob - basis/io/sockets/sockets-docs.factor
Fix permission bits
[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 { $subsection local }
8 { $subsection inet }
9 { $subsection inet4 }
10 { $subsection inet6 }
11 "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:"
12 { $subsection resolve-host } ;
13
14 ARTICLE: "network-connection" "Connection-oriented networking"
15 "Network connections can be established with this word:"
16 { $subsection <client> }
17 { $subsection with-client }
18 "Connection-oriented network servers are implemented by first opening a server socket, then waiting for connections:"
19 { $subsection <server> }
20 { $subsection accept }
21 "Server sockets are closed by calling " { $link dispose } "."
22 $nl
23 "Address specifiers have the following interpretation with connection-oriented networking words:"
24 { $list
25     { { $link local } " - Unix domain stream sockets on Unix systems" }
26     { { $link inet } " - a TCP/IP connection to a host name/port number pair which can resolve to an IPv4 or IPv6 address" }
27     { { $link inet4 } " - a TCP/IP connection to an IPv4 address and port number; no name lookup is performed" }
28     { { $link inet6 } " - a TCP/IP connection to an IPv6 address and port number; no name lookup is performed" }
29 }
30 "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."
31 $nl
32 "The " { $vocab-link "io.sockets.secure" } " vocabulary implements secure, encrypted sockets via SSL and TLS." ;
33
34 ARTICLE: "network-packet" "Packet-oriented networking"
35 "A packet-oriented socket can be opened with this word:"
36 { $subsection <datagram> }
37 "Packets can be sent and received with a pair of words:"
38 { $subsection send }
39 { $subsection receive }
40 "Packet-oriented sockets are closed by calling " { $link dispose } "."
41 $nl
42 "Address specifiers have the following interpretation with packet-oriented networking words:"
43 { $list
44     { { $link local } " - Unix domain datagram sockets on Unix systems" }
45     { { $link inet4 } " - a TCP/IP connection to an IPv4 address and port number; no name lookup is performed" }
46     { { $link inet6 } " - a TCP/IP connection to an IPv6 address and port number; no name lookup is performed" }
47 }
48 "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)." ;
49
50 ARTICLE: "network-streams" "Networking"
51 "Factor supports connection-oriented and packet-oriented communication over a variety of protocols:"
52 { $list
53     "TCP/IP and UDP/IP, over IPv4 and IPv6"
54     "Unix domain sockets (Unix only)"
55 }
56 { $subsection "network-addressing" }
57 { $subsection "network-connection" }
58 { $subsection "network-packet" }
59 { $subsection "io.sockets.secure" }
60 { $see-also "io.pipes" } ;
61
62 ABOUT: "network-streams"
63
64 HELP: local
65 { $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> } "." }
66 { $examples
67     { $code "\"/tmp/.X11-unix/0\" <local>" }
68 } ;
69
70 HELP: inet
71 { $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> } "." }
72 { $notes
73     "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."
74 }
75 { $examples
76     { $code "\"www.apple.com\" \"http\" <inet>" }
77     { $code "\"localhost\" 8080 <inet>" }
78 } ;
79
80 HELP: inet4
81 { $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> } "." }
82 { $notes
83 "Most applications do not operate on IPv4 addresses directly, and instead should use " { $link resolve-host } " to look up the address associated to a host name. Also, try to support IPv6 where possible."
84 }
85 { $examples
86     { $code "\"127.0.0.1\" 8080 <inet4>" }
87 } ;
88
89 HELP: inet6
90 { $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> } "." }
91 { $notes
92 "Most applications do not operate on IPv6 addresses directly, and instead should use " { $link resolve-host } " to look up the address associated to a host name." }
93 { $examples
94     { $code "\"::1\" 8080 <inet6>" }
95 } ;
96
97 HELP: <client>
98 { $values { "remote" "an address specifier" } { "encoding" "an encding descriptor" } { "stream" "a bidirectional stream" } { "local" "an address specifier" } }
99 { $description "Opens a network connection and outputs a bidirectional stream using the given encoding, together with the local address the socket was bound to." }
100 { $errors "Throws an error if the connection cannot be established." }
101 { $notes "The " { $link with-client } " word is easier to use in most situations." }
102 { $examples
103     { $code "\"www.apple.com\" \"http\" <inet> utf8 <client>" }
104 } ;
105
106 HELP: with-client
107 { $values { "remote" "an address specifier" } { "encoding" "an encoding descriptor" } { "quot" quotation } }
108 { $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 bound to is stored in the " { $link local-address } " variable." }
109 { $errors "Throws an error if the connection cannot be established." } ;
110
111 HELP: <server>
112 { $values  { "addrspec" "an address specifier" } { "encoding" "an encoding descriptor" } { "server" "a handle" } }
113 { $description
114     "Begins listening for network connections to a local address. Server objects responds to two words:"
115     { $list
116         { { $link dispose } " - stops listening on the port and frees all associated resources" }
117         { { $link accept } " - blocks until there is a connection, and returns a stream of the encoding passed to the constructor" }
118     }
119 }
120 { $notes
121     "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:"
122     { $code "f 1234 <inet> resolve-host" }
123     "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:"
124     { $code "\"localhost\" 1234 <inet> resolve-host" }
125     "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."
126     $nl
127     "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:"
128     { $unchecked-example
129         "f 0 <inet4> ascii <server>"
130         "[ addr>> . ] [ dispose ] bi"
131         "T{ inet4 f \"0.0.0.0\" 58901 }"
132     }
133 }
134 { $errors "Throws an error if the address is already in use, or if it if the system forbids access." } ;
135
136 HELP: accept
137 { $values { "server" "a handle" } { "client" "a bidirectional stream" } { "remote" "an address specifier" } }
138 { $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." }
139 { $errors "Throws an error if the server socket is closed or otherwise is unavailable." } ;
140
141 HELP: <datagram>
142 { $values { "addrspec" "an address specifier" } { "datagram" "a handle" } }
143 { $description "Creates a datagram socket bound to a local address. Datagram socket objects responds to three words:"
144     { $list
145         { { $link dispose } " - stops listening on the port and frees all associated resources" }
146         { { $link receive } " - waits for a packet" }
147         { { $link send } " - sends a packet" }
148     }
149 }
150 { $notes
151     "To accept UDP/IP packets from any host, use an address specifier returned by the following code, where 1234 is the desired port number:"
152     { $code "f 1234 <inet> resolve-host" }
153     "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:"
154     { $code "\"localhost\" 1234 <inet> resolve-host" }
155     "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."
156     "Datagrams are low-level binary ports that don't map onto streams, so the constructor does not use an encoding"
157 }
158 { $errors "Throws an error if the port is already in use, or if the OS forbids access." } ;
159
160 HELP: receive
161 { $values { "datagram" "a datagram socket" } { "packet" byte-array } { "addrspec" "an address specifier" } }
162 { $description "Waits for an incoming packet on the given datagram socket. Outputs the packet data, as well as the sender's address." }
163 { $errors "Throws an error if the packet could not be received." } ;
164
165 HELP: send
166 { $values { "packet" byte-array } { "addrspec" "an address specifier" } { "datagram" "a datagram socket" } }
167 { $description "Sends a packet to the given address." }
168 { $errors "Throws an error if the packet could not be sent." } ;
169
170 HELP: resolve-host
171 { $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } }
172 { $description "Resolves host names to IP addresses." } ;