1

Both Network.Socket.ByteString and Network.Socket.ByteString.Lazy have a send function.

Network.Socket.ByteString has a sendTo function, but Network.Socket.ByteString.Lazy doesn't.

How can I use Network.Socket.ByteString's sendTo with a Lazy.ByteString or Network.Socket.ByteString.Lazy's send function. (i.e. how do I tell it where to send the packet.)

Can anyone recommend a good tutorial on Haskell's Strings, BytesStrings. Lazy.ByteStrings, etc. as I find them very confusing (coming from a Java/Python background).

fadedbee
  • 42,671
  • 44
  • 178
  • 308
  • 1
    Note that `sendTo` is strict in the data sent, and so there's no real logic to passing it a lazy bytestring. That's why the function only exists on strict bytestrings. – sclv Jun 15 '12 at 16:14
  • @sclv - yours is the correct answer (mine is a solution). Could you post your comment as an answer so that I can accept it? – fadedbee Jun 17 '12 at 05:25

2 Answers2

1

The answer was to make a new function:

import Data.ByteString as BS
import Data.ByteString.Lazy as LBS

lazyToStrictBS :: LBS.ByteString -> BS.ByteString
lazyToStrictBS x = BS.concat $ LBS.toChunks x

and use it to convert the Lazy.ByteString into a normal ByteString.

Here's the converse, so that I find it when I google this problem again in future.

import Data.ByteString as BS
import Data.ByteString.Lazy as LBS

strictToLazyBS :: BS.ByteString -> LBS.ByteString
strictToLazyBS x = LBS.fromChunks [x] 
fadedbee
  • 42,671
  • 44
  • 178
  • 308
  • 1
    It is very uncommon to interact with sockets directly in most modern programs﹘which is why most applications that I know of use some form of abstraction (Like [`pipes-network`](http://hackage.haskell.org/package/pipes-network)) for lazy byte strings. Also, +1 for "so that I find it when I google this problem again in future" :) – dflemstr Jun 15 '12 at 22:13
1

Note that sendTo is strict in the data sent, and so there's no real logic to passing it a lazy bytestring. That's why the function only exists on strict bytestrings.

sclv
  • 38,665
  • 7
  • 99
  • 204