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
しかし、問題はまだあります。私は何を間違っていますか?