1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
*Project* Anubis
*Title* A simple client/server program.
*Copyright* Copyright (c) Alain Prouté 2005.
*Released*
*Author* Alain Prouté
*Overview*
This file contains a example of a server and the corresponding client. It is a
'minimalist' program, as simple as possible. In this file you learn how to use the
following functions:
function defined in
----------------------------------------------------------------------
start_server predefined.anubis
remote_IP_address_and_port predefined.anubis
ip_addr_to_string tools/basis.anubis
prompt tools/basis.anubis
reliable_write tools/basis.anubis
tcp tools/connections.anubis
to_byte_array predefined.anubis
ip_address tools/basis.anubis
connect predefined.anubis
In order to try it out, compile this file and open two consoles. In the first console,
issue the command:
simple_server 3000
In the second console, issue the command:
simple_client 127.0.0.1 3000
Now, enter some text in the client (second) console. It should appear in the server
(first) console.
You may start several clients. Of course, 3000 in this example is the port number on
which the srver is listening. You may also start several servers provided thay have
distinct listening port numbers.
*** Description.
The server starts and waits for one or several client programs to connect. When a
client program is connected, the user of this client program may input sentences using
the keyboard. Each line is transmitted to the server, and the server prints it on its
console. That's all.
There is nothing to do in order to handle several clients simultaneously. This is done
automatically by the functions defined in 'predefined.anubis'.
read tools/basis.anubis
read tools/connections.anubis
*** The server.
The server needs a 'handler'. Recall (see 'predefined.anubis') that when a client is
connected, the server starts a virtual machine for handling this client (one machine
per client). This virtual machine runs the following program, which is an 'infinite'
loop. Actually, the loop is exited when the client closes the connection.
The server handler reads bytes from the connection until a line feed ('\n') is
found. Then, it prints the bytes prefixed by the address and port of the client. It
continues to read until the connection is closed by the client.
define One
server_handler
(
RWStream connection, // the connection with the client
List(Word8) read_so_far, // the bytes read so far (in reverse order)
String from // the prefix string (for example:
// "From 127.0.0.1:12345: ")
) =
if *connection is // read one byte from the connection
// (this expression waits for the next byte or the connection
// is closed by the client)
{
failure then // the connection has been closed by the client
print(from+"Connection closed by client.\n"),
success(c) then // the byte 'c' has been read
if c = '\n'
// if this byte is '\n', it is time to print the message received from the client
then (print(from+
implode(reverse(read_so_far))+"\n"); // print the message
server_handler(connection,[],from)) // continue to wait for bytes
// otherwise, just remember the byte read and continue to wait for bytes
else server_handler(connection,[c . read_so_far],from)
}.
Notice that the recursive calls to 'server_handler' above are terminal so that the
above function is an actual loop (the stack of the virtual machine does not grow at
each call).
The function 'start_server' requires the handler in the form of a function of type:
Server -> ((RWStream) -> One)
(see 'predefined.anubis'). The 'Server' argument is not used by the handler in this
example. The next function 'make_server_handler' is just an interface so that
'start_server' can get the handler with the required type.
define Server -> ((RWStream) -> One)
make_server_handler
(
One u // this dummy argument is required because the Anubis compiler
// version 1 cannot handle definition of functions at top level
// with no argument after the name. This is due to the fact that
// version 1 identifies functions of zero operand with the result
// of applying this function to zero operand. This was an
// unfortunate design idea.
) =
(Server s) |->
(RWStream connection) |->
//
// we use 'remote_IP_address_and_port' (see 'predefined.anubis')
// for getting the address and port of the client.
//
if remote_IP_address_and_port(connection) is (a,p) then
print("New connection from "+ip_addr_to_string(a)+":"+p+"\n");
server_handler(connection,
[],
//
// and we construct the prefix string once and for all
//
"From "+ip_addr_to_string(a)+":"+p+": ").
The 'simple_server' program must be started with a port number. The function below
recalls the syntax.
define One
server_syntax
=
print("Usage: simple_server <port number>\n").
Now, here is the function generating the module 'simple_server.adm'. We need to read
the port number (which is a string), to transform it to an integer, and to start the
server. All these operations may produce errors, which are handled by error messages.
global define One
simple_server
(
List(String) args
) =
if args is
{
[ ] then server_syntax,
[h . _] then
if string_to_integer(h) is
{
failure then server_syntax,
success(port) then
if start_server(0, // listen on all network interfaces
port,
make_server_handler(unique),
(One u) |-> u) is
{
cannot_create_the_socket then print("Cannot create the socket for listening.\n"),
cannot_bind_to_port then print("Cannot bind to port "+h+".\n"),
cannot_listen_on_port then print("Cannot listen on port "+h+".\n"),
ok(server) then print("Simple Server started on port "+h+".\n")
}
}
}.
*** The client.
The client handler sends a 'prompt' to the client console, and waits for a line of
text. The function 'prompt' used below is taken from 'tools/basis.anubis'. We use
'reliable_write' (also defined in 'tools/basis.anubis') for sending the message. The
function 'reliable_write' requires a connection of type 'Connection' (defined in
'tools/connections.anubis'). This is why the constructor 'tcp' is required here. Also,
the text must be transformed into a byte array before being sent. The conversion
function 'to_byte_array' is defined in 'predefined.anubis'.
define One
client_handler
(
RWStream connection
) =
with text = prompt("simple_client> "),
if reliable_write(tcp(connection),to_byte_array(text+"\n")) is
{
failure then print("Cannot send message.\n"),
success(n) then unique
};
client_handler(connection).
Notice again that the above is an 'infinite' loop. Actually, this loop cannot be exited
within the program. It is exited only when 'anbexec' (of the client) is stopped by a
[Ctrl C]. Also notice that we must not forget to send "\n" appended to the
text. Otherwise, the server will wait indefinitely for the end of the line, and will
print nothing. This "\n" is part of our 'simple transmission protocol'.
The client program requires an IP address and port number in order to be able to find
the server. The next function recalls the syntax of 'simple_client'.
define One
client_syntax
(
String additional_message
) =
print("Usage: simple_client <IP address> <port number>\n");
print(additional_message).
Now, here is the function generating the module 'simple_client.adm'. The arguments (IP
address and port number) are first read and converted.
global define One
simple_client
(
List(String) args
) =
if args is
{
[ ] then // no argument on command line
client_syntax("Address and port number missing.\n"),
[a . t] then if t is
{
[ ] then // only one argument on command line
client_syntax("Port number missing.\n"),
[p . _] then // try to convert the IP address
// 'ip_address' is defined in 'tools/basis.anubis'
if ip_address(a) is
{
failure then
client_syntax("Incorrect IP address.\n"),
success(addr) then // try to convert the port number
if string_to_integer(p) is
{
failure then
client_syntax("Incorrect port number.\n"),
success(port) then // try to connect to server
if connect(addr,port) is
{
error(msg) then
print("Cannot connect to "+a+":"+p+"\n"),
ok(connection) then
//
// connection to server is OK
// just run the client handler
//
print("Connected to "+a+":"+p+"\n");
client_handler(connection)
}
}
}
}
}. |
Partager