haskell - How to properly close network connections with network-conduit? -
larn basics of conduit
library, used network-conduit
create simple echo server:
import control.monad.io.class import qualified data.bytestring.char8 bs import data.conduit import data.conduit.network -- conduit print input receives on console -- , passes through. echo :: (monadio m) => conduit bs.bytestring m bs.bytestring echo = yield (bs.pack "anything type echoed back.\n") -- print received info console well: awaitforever (\x -> liftio (bs.putstr x) >> yield x) echoapp :: (monadio m) => application m echoapp appdata = appsource appdata $= echo $$ appsink appdata -- hear on port 4545: main :: io () main = runtcpserver (serversettings 4545 hostany) echoapp
it wanted, when client closes part of connection, server still waiting input instead of writing out remaining info , closing sending part of connection too:
class="lang-sh prettyprint-override">$ nc localhost 4545 <<<"hello world!" type echoed back. hello world!
i tried removing echo
, just
echoapp appdata = appsource appdata $$ appsink appdata
but problem still there. doing wrong?
i'm not sure mean "the server won't respond it"? i'd guess you're expecting server shut downwards after client disconnects. if so, that's not intention of library: continues server connections in infinite loop long go on coming in. using addcleanup
, can see individual connection handlers in fact terminate, e.g.:
echo :: (monadio m) => conduit bs.bytestring m bs.bytestring echo = addcleanup (const $ liftio $ putstrln "stopping") $ yield (bs.pack "anything type echoed back.\n") -- print received info console well: awaitforever (\x -> liftio (bs.putstr x) >> yield x)
haskell conduit network-connection
No comments:
Post a Comment