How to get IO String from Network.Socket.ByteString.recv method

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get IO String from Network.Socket.ByteString.recv method

Dinesh Amerasekara
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to get IO String from Network.Socket.ByteString.recv method

Daniel Bergey-2
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 <[hidden email]> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to get IO String from Network.Socket.ByteString.recv method

Steven Leiva
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 <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners