[Haskell-cafe] Simple network client
jules at jellybean.co.uk
Wed Jan 30 07:51:58 EST 2008
Timo B. Hübel wrote:
> On Wednesday 30 January 2008 13:32:42 you wrote:
>> Timo B. Hübel wrote:
>>> On Wednesday 30 January 2008 13:03:27 you wrote:
>>>> Just don't use hGetContents in any serious code, or any program longer
>>>> than 4 lines.
>>> What else do you suggest? I just want to read something out of the socket
>>> without knowing it's length beforehand (my example here used ordinary
>>> Strings, but actually I want to do it with ByteStrings).
>> I strongly suspect for your example you want solution 1 and hGetLine,
>> though. (Which works just as well with or without ByteString)
> Okay, but then I have to make sure that my strings won't contain any newline
> characters, right? If this is the case, another question raises up: I am
> using Data.Binary to do the serialization of my data structures to
> ByteString, so does anybody know if this makes guarantees about newline
> characters in the resulting ByteString?
> Otherwise I would go for the "transmit the length of what to expect"-solution.
Fortunately there is an easy way to hide newlines.
That will wrap newlines as \n, as well as coping with other odd
characters like NULL which might upset a C library (if you're talking to
C at any point).
Then you use "read" on the far end.
Otherwise, you make your protocol more sophisticated in some way, like
"messages is ended by a line which only contains '.'", which is the SMTP
and, AFAICR, NNTP solution to this particular sub-problem.
Of course show/read *is* one way of making your protocol more
sophisticated. It just happens to be a really easy hack, for haskell
More information about the Haskell-Cafe