如何从Haskell中的Socket读取字节

时间:2021-04-03 22:34:30

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

我正在尝试从Haskell中的套接字读取一些字节。基本上我想做一些与此相当的事情:

client_socket.recv(255) #(Python)

What's the best way of doing it?

这样做的最佳方法是什么?

2 个解决方案

#1


13  

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.

有Network.Socket,它有recvFrom和recvBufFrom。第一个假定你想要一个String,如果你想要二进制数据,你肯定不想要它。第二个使用指针,您可能不想处理它。还有socketToHandle,这非常有用。

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

但是,我的建议是network-bytestring库。它支持惰性和严格的字节串。 http://hackage.haskell.org/package/network-bytestring

#2


2  

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

对于这种初学者问题,首先查看RWH并不是一个坏主意。

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.

作为一般的经验法则,您应该始终查看Hackage的库和文档。要搜索某个功能,Hayoo和Hoogle是您的朋友。

#1


13  

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.

有Network.Socket,它有recvFrom和recvBufFrom。第一个假定你想要一个String,如果你想要二进制数据,你肯定不想要它。第二个使用指针,您可能不想处理它。还有socketToHandle,这非常有用。

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

但是,我的建议是network-bytestring库。它支持惰性和严格的字节串。 http://hackage.haskell.org/package/network-bytestring

#2


2  

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

对于这种初学者问题,首先查看RWH并不是一个坏主意。

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.

作为一般的经验法则,您应该始终查看Hackage的库和文档。要搜索某个功能,Hayoo和Hoogle是您的朋友。