Simple sockets sample?

Dominic Cooney dominic@dcooney.com
Mon, 28 Jan 2002 17:41:22 +1000


I realize that my use of the API was incredibly na=EFve and that I =
should
have been using h* methods with the handle returned from connectTo. But
it is connectTo that is failing as described.

It fails interactively too with ghci, and regardless of using Server
"http" or PortNumber 80.

The revised code below unfortunately gives the following error:

Main> main
*** Exception: does not exist
Action: getProtocolByName
Reason: no such protocol entry

The code is:

> module Main(main) where

> import IO
> import Socket

> main :: IO ()
> main =3D do
>	 h <- connectTo "www.yahoo.com" (PortNumber 80)
>	 hPutStrLn h "GET / HTTP/1.0\n"
>	 response <- hGetContents h
>	 putStrLn response

I suppose the wits will tell me to use Linux instead? Sigh...

Dominic Cooney

-----Original Message-----
From: petersen@redhat.com [mailto:petersen@redhat.com] On Behalf Of Jens
Petersen
Sent: Monday, 28 January 2002 4:33 PM
To: Dominic Cooney
Cc: haskell@haskell.org
Subject: Re: Simple sockets sample?

"Dominic Cooney" <dominic@dcooney.com> writes:

> Does anybody know of a simple GHC Socket sample? I want to retrieve an
> HTTP URL, but I keep getting errors like this:

Funny I was trying to do the same this morning on the way to
work.

> A minimal program that exhibits the problem is below. I am using GHC
> 5.02 on Win XP Pro. When I enter the request via telnet, I do get a
> response.
>=20
> I assume I am making some terrible newbie mistake, so if anyone could
> point it out it would be appreciated.

> > main =3D withSocketsDo $ do {
> >	connectTo host port;
> >	sendTo host port "GET / HTTP/1.0\r\n\r\n";
> >	response <- recvFrom host port;
> >	putStrLn response

Hmmm, I was trying something more like (under Linux):

        do
        h <- connectTo host port
        hPutStrLn h "GET / HTTP/1.0\n"
        response <- hGetContents h
        putStr response

Unfortunately this hangs waiting for the response.
Don't know why yet...

Jens