Sending binary files through a socket

Ivan Salazar ivan.salazarv at gmail.com
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...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20080414/fa154cc9/attachment-0001.htm


More information about the Glasgow-haskell-users mailing list