Where is Network.Socket.ByteString.Lazy's sendTo?

拟墨画扇 提交于 2020-01-03 03:10:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!