问题
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).
回答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.
回答2:
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]
来源:https://stackoverflow.com/questions/11052388/where-is-network-socket-bytestring-lazys-sendto