1 | [[tags: manual]] |
---|
2 | [[toc:]] |
---|
3 | |
---|
4 | == Unit tcp |
---|
5 | |
---|
6 | This unit provides basic facilities for communicating over TCP sockets. |
---|
7 | The socket interface should be mostly compatible to the one found in |
---|
8 | PLT Scheme. |
---|
9 | |
---|
10 | This unit uses the {{extras}} unit. |
---|
11 | |
---|
12 | All errors related to failing network operations will raise a condition |
---|
13 | of kind {{(exn i/o network)}}. |
---|
14 | |
---|
15 | |
---|
16 | === tcp-listen |
---|
17 | |
---|
18 | [procedure] (tcp-listen TCPPORT [BACKLOG [HOST]]) |
---|
19 | |
---|
20 | Creates and returns a TCP listener object that listens for connections on {{TCPPORT}}, which |
---|
21 | should be an exact integer. {{BACKLOG}} specifies the number of maximally pending |
---|
22 | connections (and defaults to 4). If the optional argument {{HOST}} is given and not |
---|
23 | {{#f}}, then only incoming connections for the given host (or IP) are accepted. |
---|
24 | |
---|
25 | |
---|
26 | === tcp-listener? |
---|
27 | |
---|
28 | [procedure] (tcp-listener? X) |
---|
29 | |
---|
30 | Returns {{#t}} if {{X}} is a TCP listener object, or {{#f}} otherwise. |
---|
31 | |
---|
32 | |
---|
33 | === tcp-close |
---|
34 | |
---|
35 | [procedure] (tcp-close LISTENER) |
---|
36 | |
---|
37 | Reclaims any resources associated with {{LISTENER}}. |
---|
38 | |
---|
39 | |
---|
40 | === tcp-accept |
---|
41 | |
---|
42 | [procedure] (tcp-accept LISTENER) |
---|
43 | |
---|
44 | Waits until a connection is established on the port on which |
---|
45 | {{LISTENER}} is listening and returns two values: an input- and |
---|
46 | output-port that can be used to communicate with the remote |
---|
47 | process. The current value of {{tcp-accept-timeout}} is used to |
---|
48 | determine the maximal number of milliseconds (if any) to wait |
---|
49 | until a connection is established. When a client connects any |
---|
50 | read- and write-operations on the returned ports will use the |
---|
51 | current values (at the time of the connection) of {{tcp-read-timeout}} |
---|
52 | and {{tcp-write-timeout}}, respectively, to determine the maximal |
---|
53 | number of milliseconds to wait for input/output before a timeout |
---|
54 | error is signalled. |
---|
55 | |
---|
56 | Note: this operation and any I/O on the ports returned will not block |
---|
57 | other running threads. |
---|
58 | |
---|
59 | |
---|
60 | === tcp-accept-ready? |
---|
61 | |
---|
62 | [procedure] (tcp-accept-ready? LISTENER) |
---|
63 | |
---|
64 | Returns {{#t}} if there are any connections pending on {{LISTENER}}, or {{#f}} |
---|
65 | otherwise. |
---|
66 | |
---|
67 | |
---|
68 | === tcp-listener-port |
---|
69 | |
---|
70 | [procedure] (tcp-listener-port LISTENER) |
---|
71 | |
---|
72 | Returns the port number assigned to {{LISTENER}} (If you pass {{0}} to {{tcp-listen}}, |
---|
73 | then the system will choose a port-number for you). |
---|
74 | |
---|
75 | === tcp-listener-fileno |
---|
76 | |
---|
77 | [procedure] (tcp-listener-fileno LISTENER) |
---|
78 | |
---|
79 | Returns the file-descriptor associated with {{LISTENER}}. |
---|
80 | |
---|
81 | |
---|
82 | === tcp-connect |
---|
83 | |
---|
84 | [procedure] (tcp-connect HOSTNAME [TCPPORT]) |
---|
85 | |
---|
86 | Establishes a client-side TCP connection to the machine with the name |
---|
87 | {{HOSTNAME}} (a string) at {{TCPPORT}} (an exact integer) and returns |
---|
88 | two values: an input- and output-port for communicating with the |
---|
89 | remote process. The current value of {{tcp-connect-timeout}} is used |
---|
90 | to determine the maximal number of milliseconds (if any) to wait until |
---|
91 | the connection is established. When the connection takes place any |
---|
92 | read- and write-operations on the returned ports will use the current |
---|
93 | values (at the time of the call to {{tcp-connect}}) of {{tcp-read-timeout}} and |
---|
94 | {{tcp-write-timeout}}, respectively, to determine the maximal number |
---|
95 | of milliseconds to wait for input/output before a timeout error is |
---|
96 | signalled. |
---|
97 | |
---|
98 | If the {{TCPPORT}} is omitted, the port is parsed from the {{HOSTNAME}} string. The format expected is {{HOSTNAME:PORT}}. The {{PORT}} can either be a string representation of an integer or a service name which is translated to an integer using the POSIX function [[http://www.opengroup.org/onlinepubs/009695399/functions/getservbyname.html|{{getservbyname}}]]. |
---|
99 | |
---|
100 | Note: any I/O on the ports returned will not block other running threads. |
---|
101 | |
---|
102 | |
---|
103 | === tcp-addresses |
---|
104 | |
---|
105 | [procedure] (tcp-addresses PORT) |
---|
106 | |
---|
107 | Returns two values for the input- or output-port {{PORT}} (which should be a port returned |
---|
108 | by either {{tcp-accept}} or {{tcp-connect}}): the IP address of the local and the remote |
---|
109 | machine that are connected over the socket associated with {{PORT}}. The returned addresses |
---|
110 | are strings in {{XXX.XXX.XXX.XXX}} notation. |
---|
111 | |
---|
112 | |
---|
113 | === tcp-port-numbers |
---|
114 | |
---|
115 | [procedure] (tcp-port-numbers PORT) |
---|
116 | |
---|
117 | Returns two values for the input- or output-port {{PORT}} (which should be a port returned |
---|
118 | by either {{tcp-accept}} or {{tcp-connect}}): the TCP port numbers of the local and the remote |
---|
119 | machine that are connected over the socket associated with {{PORT}}. |
---|
120 | |
---|
121 | |
---|
122 | === tcp-abandon-port |
---|
123 | |
---|
124 | [procedure] (tcp-abandon-port PORT) |
---|
125 | |
---|
126 | Marks the socket port {{PORT}} as abandoned. This is mainly useful to close down a port |
---|
127 | without breaking the connection. |
---|
128 | |
---|
129 | |
---|
130 | === tcp-buffer-size |
---|
131 | |
---|
132 | [parameter] tcp-buffer-size |
---|
133 | |
---|
134 | Sets the size of the output buffer. By default no output-buffering for |
---|
135 | TCP output is done, but to improve performance by minimizing the |
---|
136 | number of TCP packets, buffering may be turned on by setting this |
---|
137 | parameter to an exact integer greater zero. A buffer size of zero or {{#f}} |
---|
138 | turns buffering off. The setting of this parameter takes effect at the time |
---|
139 | when the I/O ports for a particular socket are created, i.e. when {{tcp-connect}} |
---|
140 | or {{tcp-accept}} is called. |
---|
141 | |
---|
142 | Note that since output is not immediately written to the associated socket, you |
---|
143 | may need to call {{flush-output}}, once you want the output to be transmitted. |
---|
144 | Closing the output port will flush automatically. |
---|
145 | |
---|
146 | === tcp-read-timeout |
---|
147 | |
---|
148 | [parameter] tcp-read-timeout |
---|
149 | |
---|
150 | Determines the timeout for TCP read operations in milliseconds. A timeout of |
---|
151 | {{#f}} disables timeout checking. The default read timeout is 60000, i.e. |
---|
152 | 1 minute. |
---|
153 | |
---|
154 | === tcp-write-timeout |
---|
155 | |
---|
156 | [parameter] tcp-write-timeout |
---|
157 | |
---|
158 | Determines the timeout for TCP write operations in milliseconds. A timeout of |
---|
159 | {{#f}} disables timeout checking. The default write timeout is 60000, i.e. |
---|
160 | 1 minute. |
---|
161 | |
---|
162 | === tcp-connect-timeout |
---|
163 | |
---|
164 | [parameter] tcp-connect-timeout |
---|
165 | |
---|
166 | Determines the timeout for {{tcp-connect}} operations in milliseconds. A timeout of |
---|
167 | {{#f}} disables timeout checking and is the default. |
---|
168 | |
---|
169 | |
---|
170 | === tcp-accept-timeout |
---|
171 | |
---|
172 | [parameter] tcp-accept-timeout |
---|
173 | |
---|
174 | Determines the timeout for {{tcp-accept}} operations in milliseconds. A timeout of |
---|
175 | {{#f}} disables timeout checking and is the default. |
---|
176 | |
---|
177 | |
---|
178 | === Example |
---|
179 | |
---|
180 | A very simple example follows. Say we have the two files {{client.scm}} |
---|
181 | and {{server.scm}}: |
---|
182 | |
---|
183 | <enscript highlight=scheme> |
---|
184 | ; client.scm |
---|
185 | (declare (uses tcp)) |
---|
186 | (define-values (i o) (tcp-connect "localhost" 4242)) |
---|
187 | (write-line "Good Bye!" o) |
---|
188 | (print (read-line i)) |
---|
189 | </enscript> |
---|
190 | |
---|
191 | <enscript highlight=scheme> |
---|
192 | ; server.scm |
---|
193 | (declare (uses tcp)) |
---|
194 | (define l (tcp-listen 4242)) |
---|
195 | (define-values (i o) (tcp-accept l)) |
---|
196 | (write-line "Hello!" o) |
---|
197 | (print (read-line i)) |
---|
198 | (close-input-port i) |
---|
199 | (close-output-port o) |
---|
200 | </enscript> |
---|
201 | |
---|
202 | % csc server.scm |
---|
203 | % csc client.scm |
---|
204 | % ./server & |
---|
205 | % ./client |
---|
206 | Good Bye! |
---|
207 | Hello! |
---|
208 | |
---|
209 | Previous: [[Unit utils]] |
---|
210 | |
---|
211 | Next: [[Unit lolevel]] |
---|