[Haskell-cafe] hSetEncoding on socket handles

David Powell david at drp.id.au
Tue May 11 20:56:48 EDT 2010


Greetings,

I am having trouble sending unicode characters as utf8 over a socket handle.
Despite setting the encoding on the socket handle to utf8, it still seems to
use some other encoding when writing to the socket.  It works correctly when
writing to stdout, but not to a socket handle.  I am using ghc 6.12.1 and
network-2.2.1.7.  I can get it to work using System.IO.UTF8, but I was under
the impression this was no longer necessary?

I also don't seem to understand the interaction between hSetEncoding and
hSetBinaryMode because if I set the binary mode to 'False' and the encoding
to
utf8 on the socket, then when writing to the socket the string seems to be
truncated at the first non-ascii codepoint.

Here is a test snippet, which can be used with netcat as a listening server
(ie. nc -l 1234).

> import System.IO
> import Network
> main = do
>  let a="λ"
>  s <- connectTo "127.0.0.1" (PortNumber 1234)
>  hSetEncoding s utf8
>  hSetEncoding stdout utf8
>  hPutStrLn s a
>  putStrLn a
>  hClose s

Thanks,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100511/bef21d65/attachment.html


More information about the Haskell-Cafe mailing list