Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read bytes from a Socket in Haskell

I'm trying to read a number of Bytes from a socket in Haskell. Bascially I want to do something equivalent to this:

client_socket.recv(255) #(Python)

What's the best way of doing it?

like image 981
MattyW Avatar asked Feb 01 '10 06:02

MattyW


2 Answers

There's Network.Socket, which has recvFrom and recvBufFrom. The first one assumes you want a String, which you certainly don't want if you want binary data. The second one uses a pointer, which you probably don't want to deal with. There's also socketToHandle, which is very useful.

However, my recommendation is the network-bytestring library. It supports both lazy and strict bytestrings. http://hackage.haskell.org/package/network-bytestring

like image 84
Dietrich Epp Avatar answered Nov 06 '22 12:11

Dietrich Epp


For this kind of beginner questions, it's not a bad idea to check out RWH first.

And as a general rule of thumb, you should always look at Hackage for libraries and documentation. To search for a function, Hayoo and Hoogle are your friends.

like image 2
Wei Hu Avatar answered Nov 06 '22 12:11

Wei Hu