sockets and handles in ghc

Jurriaan Hage jur@cs.uu.nl
Mon, 30 Jun 2003 13:24:57 +0200


On Friday, June 27, 2003, at 07:08 PM, Volker Stolz wrote:

> [Moving to glasgow-haskell-users@haskell.org]
>
> In local.haskell, you wrote:
>> I've been using sockets and handles with ghc-5.04.3.
>> The strange thing is now that when I make a handle
>> out of a socket and ask whether the handle is readable
>> or writable, it returns True for the former and False
>> for the latter, although sockets are bidirectional. And yes,
>
> Do you have a small sample program which shows this behaviour?
> Can you try ghc-6? There have been some updates, but I cannot tell
> if this was a particular bug that got fixed.
>
> Regards,
>   Volker
Installed GHC 6.0 (on MacOsX) and recompiled and it gave the same 
results.
I hope I did everything as it should there, I am not much
of an expert. The problem with the code fragment
is that it is part of a very large program, but I can give the 
pertinent parts (which
I guess can not be compiled by themselves).


sendLogString :: String -> Bool -> IO ()
sendLogString message loggerDEBUGMODE = withSocketsDo (rec 0)

  where
     rec i = do
              handle <- connectTo loggerHOSTNAME (PortNumber 
(fromIntegral loggerPORTNUMBER))
              hSetBuffering handle (BlockBuffering (Just 1024))
              sendToAndFlush handle message loggerDEBUGMODE
           `catch`
               \exception ->
                  if i+1 >= loggerTRIES
                    then debug ( "Could not make a connection: no send 
(" ++ show exception ++ ")" ) loggerDEBUGMODE
                    else do debug ( "Could not make a connection: 
sleeping (" ++ show exception ++ ")" ) loggerDEBUGMODE
                            threadDelay loggerDELAY
                            rec (i+1)

sendToAndFlush :: Handle        -- Hostname
                -> String        -- Message to send
                -> Bool          -- Debug logger?

                -> IO ()
sendToAndFlush handle msg loggerDEBUGMODE = do
   hPutStr handle msg
   hPutStr handle loggerSPLITSTRING
   hFlush handle
-- The following two lines now output 'not writable and readable'
   b1 <- hIsWritable handle
   b2 <- hIsReadable handle
   putStrLn ((if b1 then "writable" else "not writable") ++ " and " ++
       (if b2 then "readable" else "not readable"))
   debug "Waiting for a handshake"  loggerDEBUGMODE
   handshake <- getRetriedLine 0
   debug ("Received a handshake: " ++ show handshake) loggerDEBUGMODE
   where
     getRetriedLine i =
       do
         line <- hGetLine handle
         return line
       `catch`
         \_ ->
           if i+1 >= loggerTRIES
             then do
                    debug "Did not receive anything back" loggerDEBUGMODE
                    return ""
             else do
                    debug "Waiting to try again" loggerDEBUGMODE
                    threadDelay loggerDELAY
                    getRetriedLine (i+1)

Hope this helps.

Jur
--
http://www.cs.uu.nl/people/jur/progrock.html -- jur@cs.uu.nl
If e-mail does not work try jurriaanhage@hotmail.com