File: HTTPSample.ML

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (23 lines) | stat: -rw-r--r-- 863 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(* Example code to read from the Poly/ML website. *)

let
val s : Socket.active INetSock.stream_sock = INetSock.TCP.socket();
val SOME poly = NetHostDB.getByName "www.polyml.org"
val addr = INetSock.toAddr(NetHostDB.addr poly, 80)
(* There seems to be a bug in Solaris 8 which means that select indicates that there is
   data to read even when there isn't. *)
fun readit () =
   case Socket.select{rds = [Socket.sockDesc s], wrs=[], exs=[], timeOut=SOME(Time.fromSeconds 10)} of
       { rds = [], ...} => ()
   |  _ => let val text = Socket.recvVec(s, 1)
           in if Word8Vector.length text = 0 then ()
                else ( print(Byte.bytesToString text); readit() )
           end
in
Socket.connect (s, addr);
Socket.sendVec(s, Word8VectorSlice.full(Byte.stringToBytes "GET / HTTP/1.1\r\nHost: www.polyml.org\r\n\r\n"));

readit();

Socket.close s
end;