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

Steven Leiva leiva.steven at gmail.com
Tue Jun 5 19:05:00 UTC 2018


A bit late to the party, but to build on the previous answer, you're going
to run into the same problem with the Text data type, which has both a lazy
variant and a strict (?) variant.

The functions to convert from/to the strict/lazy variant are usually in the
lazy module:

*Data.Text.Lazy.toStrict* and *Data.Text.Lazy.fromStrict*.

*Data.ByteString.Lazy.toStrict* and *Data.ByteString.Lazy.toStrict*.

It's a common use-case to want to save the Text / ByteString to a file, and
in those cases you can find helper functions such as
*Data.ByteString.Lazy.writeFile*.



On Sun, May 20, 2018 at 6:56 PM, Daniel Bergey <bergey at teallabs.org> wrote:

> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Steven Leiva
305.528.6038
leiva.steven at gmail.com
http://www.linkedin.com/in/stevenleiva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180605/b19da434/attachment.html>


More information about the Beginners mailing list