要了解conduit
库的基础知识,我用network-conduit
做一个简单的回显服务器:如何通过网络管道正确关闭网络连接?
import Control.Monad.IO.Class
import qualified Data.ByteString.Char8 as BS
import Data.Conduit
import Data.Conduit.Network
-- A conduit that print the input it receives on the console
-- and passes it through.
echo :: (MonadIO m) => Conduit BS.ByteString m BS.ByteString
echo = do
yield (BS.pack "Anything you type will be echoed back.\n")
-- Print the received data to the console as well:
awaitForever (\x -> liftIO (BS.putStr x) >> yield x)
echoApp :: (MonadIO m) => Application m
echoApp appData = appSource appData $= echo $$ appSink appData
-- Listen on port 4545:
main :: IO()
main = runTCPServer (serverSettings 4545 HostAny) echoApp
它做什么我想要的,但是当客户端关闭连接的一部分,服务器仍然在等待输入,而不是写出来的任何剩余的数据和关闭连接它的发送部分太:
$ nc localhost 4545 <<<"Hello world!"
Anything you type will be echoed back.
Hello world!
我试图消除echo
而且只做
echoApp appData = appSource appData $$ appSink appData
但问题仍然存在。我究竟做错了什么?
谢谢迈克尔。我期待服务器在远程客户端关闭发送一半时停止读取套接字。事实证明,问题出在'nc',而不是管道库。 'nc'不关闭发送端的一半。 – 2013-02-11 12:26:13
只是好奇什么样的netcat被使用 - openbsd的或gnu的重写? – 2013-02-11 15:29:34