[Haskell-cafe] Simple network client

Jules Bean jules at jellybean.co.uk
Wed Jan 30 08:09:31 EST 2008


Timo B. Hübel wrote:
> On Wednesday 30 January 2008 13:51:58 you wrote:
>>> 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.
>>
>> Use "show"
>>
>> 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.
> 
> This sounds good, but don't I throw away all (possible) performance gains of 
> transmitting ByteStrings directly when using show/read to convert them to 
> ordinary strings and back?

Probably not all of them, but some of them, definitely.

If you want to transmit an arbitrary bytestring then I'm pretty sure 
that transmitting a length word first is the way to go. An arbitrary 
bytestring can have any value in it, so there are no values left to act 
as delimiters :) You'd have to have some kind of escaping mechanism, 
like show, which is expensive.

Jules


More information about the Haskell-Cafe mailing list