[Haskell-beginners] How to get IO String from Network.Socket.ByteString.recv method

Daniel Bergey bergey at teallabs.org
Sun May 20 23:56:47 UTC 2018


Network.Socket.ByteString.recv uses the strict ByteString from
Data.ByteString, not the lazy one from Data.ByteString.Lazy.  So you
want the `unpack` from Data.ByteString.Char8, rather than
Data.ByteString.Lazy.Char8.

I never remember which functions return strict or lazy ByteString.  I
find the easiest way to check is to open the online docs and see where
the `ByteString` link points:

https://hackage.haskell.org/package/network-2.7.0.0/docs/Network-Socket-ByteString.html#v:recv

points to:

https://hackage.haskell.org/package/bytestring-0.10.8.2/docs/Data-ByteString.html#t:ByteString

hope this helps,
bergey

On 2018-05-20 at 07:52, Dinesh Amerasekara <ddinesh31 at yahoo.com> wrote:
> Hi,
>
> I am unable to compile the below code.
>
> import Network.Socket hiding(recv) 
> import Network.Socket.ByteString as S (recv) 
> import qualified Data.ByteString.Lazy.Char8 as Char8  
>
> getMessage :: Socket -> IO String 
> getMessage sock =  Char8.unpack <$> S.recv sock 8888
>
> It gives the below error.
>
> Couldn't match type ‘Data.ByteString.Internal.ByteString’
>                      with ‘ByteString’
> NB: ‘ByteString’ is defined in ‘Data.ByteString.Lazy.Internal’
>     ‘Data.ByteString.Internal.ByteString’
>         is defined in ‘Data.ByteString.Internal’
>     Expected type: IO ByteString
>     Actual type: IO Data.ByteString.Internal.ByteString
>
> In the second argument of ‘(<$>)’, namely ‘recv sock 8888’
> In the expression: unpack <$> recv sock 8888
> In an equation for ‘getMsg’:
>       getMsg sock = unpack <$> recv sock 8888
>
> Can somebody tell me how I can return the IO String using
> Network.Socket.ByteString.recv?
>
> Best Regards,
> Dinesh.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


More information about the Beginners mailing list