Network.Socketを試す

30分プログラム、その356。Unix/Linuxプログラミング理論と実践を読んでたら、じかにソケットを触りたくなったので、試してみた。
gethostbynameを呼ぶ方法が分からなかったので、getAddrInfoでごまかしてみた。

使い方

$ get example.com /
<HTML>
<HEAD>
  <TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>You have reached this web page by typing &quot;example.com&quot;,
&quot;example.net&quot;,
  or &quot;example.org&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not available
  for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
  2606</a>, Section 3.</p>
</BODY>
</HTML>

ソースコード

import System
import Network.Socket

get url path = do server  <- socket AF_INET Stream 0
                  info:_  <- getAddrInfo Nothing (Just url) (Just"http")
                  connect server $ addrAddress info
                  send server $ "GET " ++ path ++ "\n"
                  recv server 1024

main = do (url:path:_) <- getArgs
          get url path >>= putStr