Created
December 19, 2012 01:14
-
-
Save siritori/4333616 to your computer and use it in GitHub Desktop.
HTTP client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bufsize = 1024;; | |
let port = 80;; | |
let http_get sock = | |
let rec http_get' sock acc = | |
let buf = String.create bufsize in | |
if Unix.read sock buf 0 (String.length buf) = 0 then acc | |
else http_get' sock acc ^ buf | |
in http_get' sock "";; | |
let () = | |
let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in | |
let ia = Unix.inet_addr_of_string "219.94.252.252" in | |
let addr = Unix.ADDR_INET(ia, port) in | |
Unix.connect sock addr; | |
let query = "GET / HTTP/1.0\r\n\r\n" in | |
ignore (Unix.write sock query 0 (String.length query)); | |
print_endline (http_get sock); | |
(* | |
let buf = String.create bufsize in | |
while (Unix.read sock buf 0 (String.length buf)) <> 0 do | |
print_endline buf | |
done; | |
*) | |
Unix.close sock;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buf そのまま足すと bufsize バイト必ず足しちゃうので read の結果のバイト数だけ足す
足し方は (^) はコピーを作るので駄目すぎ絶対駄目。 Buffer.add_substring 使う