Sending binary files through a socket

Ivan Salazar ivan.salazarv at
Mon Apr 14 21:19:37 EDT 2008

Hello, I'm having a lot of trouble sending binary files through a socket
(I'm trying to program a very minimalistic webserver) but I just can't find
a way to read and send binary files, even though, plain text and html
documents are sent correctly.

My code looks something like this:

sendResource handle name =
    do exist <- doesFileExist name
       isFile <- liftM not (doesDirectoryExist name)
       if exist && isFile
           then do resource <- Data.ByteString.readFile name
                   hPutStr handle (header ++ fileType ++ "\n")
                   hSetBinaryMode handle True
                   hPutStr handle resource
           else do hPutStr handle page404
    where fileType = maybe ("text/plain") (flip const [])
                     (lookup (takeExtension name) extensions)

Thanks for your help in advance.

PS: Yes, I'm terribly newbie to Haskell.
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Glasgow-haskell-users mailing list