Re: Simple sockets sample?

2002-01-28 Thread Sigbjorn Finne

I don't believe there's a bug in here, only perhaps in
the way the Socket functions are used:

main = withSocketsDo $ do
d <- connectTo host port  -- (1)
sendTo host port "GET / HTTP/1.0\r\n\r\n"; -- (2)
response <- recvFrom host port; -- (3)
putStrLn response
 where
host = "localhost"
port = Service "http"

(1) is creating a TCP connection, only never to use the
handle returned. (2) does effectively the same, but
closes the TCP connection after having transmitted
the HTTP request -- the web server presumably sees
this, but also sees that the client closes the socket.
(3) waits for an incoming connection from the web
server, which won't happen, as that's not how HTTP
works.

Someone suggested the following variation:

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

Almost, I'd make two changes to have it work on all
platforms:

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


withSocketsDo is reqd to start up WinSock & must always
be used if you want your socket code to work under Win32
(removing the need for explicit user-level initialisation is on
my ToDo list, however). Having to explicitly flush out the
request doesn't look quite right though, I'll have a closer
look.

hth
--sigbjorn

- Original Message -
From: "Simon Marlow" <[EMAIL PROTECTED]>
To: "Dominic Cooney" <[EMAIL PROTECTED]>; "Jens Petersen"
<[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Monday, January 28, 2002 04:36
Subject: RE: Simple sockets sample?


> > I realize that my use of the API was incredibly naïve 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
>
> There were some problems with Sockets on Windows platforms in versions
5.02 and 5.02.1 of GHC.  Version 5.02.2 should be better behaved, although I
admit I haven't tested this personally.
>
> Cheers,
> Simon
>
> ___
> Glasgow-haskell-bugs mailing list
> [EMAIL PROTECTED]
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs


___
Glasgow-haskell-bugs mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs



RE: Simple sockets sample?

2002-01-28 Thread Simon Marlow

> I realize that my use of the API was incredibly naïve 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

There were some problems with Sockets on Windows platforms in versions 5.02 and 5.02.1 
of GHC.  Version 5.02.2 should be better behaved, although I admit I haven't tested 
this personally.

Cheers,
Simon

___
Glasgow-haskell-bugs mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs