File: Test178.ML

package info (click to toggle)
polyml 5.8.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 57,736 kB
  • sloc: cpp: 44,918; ansic: 26,921; asm: 13,495; sh: 4,670; makefile: 610; exp: 525; python: 253; awk: 91
file content (35 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download
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
(* Test select call.  Modified from Test083 on poll. *)
fun verify true = ()
|   verify false = raise Fail "wrong";

val x = INetSock.TCP.socket(): Socket.passive INetSock.stream_sock
and y = INetSock.TCP.socket(): Socket.active INetSock.stream_sock;
val SOME me = NetHostDB.getByName "localhost";
val localhost = NetHostDB.addr me;
Socket.bind(x, INetSock.toAddr(localhost, 0));
Socket.listen(x, 5);
Socket.acceptNB x;

val xDesc = Socket.sockDesc x and yDesc = Socket.sockDesc y;

(* Only test y.  On Windows x does not appear but in Linux both y appears in both read and write. *)
val {rds, wrs, exs} = Socket.select{ rds=[xDesc], wrs=[xDesc], exs=[xDesc], timeout=SOME(Time.fromSeconds 1)};

verify(null rds andalso null wrs andalso null exs);

let
    val (_, port) = INetSock.fromAddr(Socket.Ctl.getSockName x)
in
    Socket.connectNB(y, INetSock.toAddr(localhost, port))
end;

val {rds, wrs, exs} = Socket.select{ rds=[xDesc, yDesc], wrs=[xDesc, yDesc], exs=[xDesc, yDesc], timeout=SOME(Time.fromSeconds 1)};

verify(Socket.sameDesc(hd rds, xDesc));
verify(Socket.sameDesc(hd wrs, yDesc));

val SOME(sock1, _) = Socket.acceptNB x;
val NONE = Socket.acceptNB x;
Socket.close sock1;
Socket.close x;
Socket.close y;