我正在尝试使用以下代码(代码是here的副本)。问题是,当我用ctrl + c关闭服务器并尝试再次运行时,我得到: * 异常:bind:资源繁忙(地址已在使用中)。
在listenOn的文档中写道:注意:为避免在GHC-Users邮件列表中多次弹出“已使用的地址”问题,我们在侦听套接字上设置了ReuseAddrsocket选项。如果您不想要此行为,请使用较低级别的听取。请问我该如何解决这个问题? (ghci版本7.6.3)
import Network (listenOn, accept, PortID(..))
import Network.Socket (Socket, isSupportedSocketOption, SocketOption(..))
import System.IO (hSetBuffering, hGetLine, hPutStrLn, BufferMode(..), Handle)
import Control.Concurrent (forkIO)
echoImpl :: Handle -> IO ()
echoImpl client = do
line <- hGetLine client
hPutStrLn client line
echoImpl client
clientHandler :: Socket -> IO ()
clientHandler socket = do
(client, _, _) <- accept socket
hSetBuffering client NoBuffering
forkIO $ echoImpl client
clientHandler socket
felix :: IO ()
felix = do
let reuseAddrSupported = isSupportedSocketOption ReuseAddr
putStrLn $ "ReuseAddr: " ++ show reuseAddrSupported
socket <- listenOn $ PortNumber 5002
putStrLn $ "Echo server started .."
clientHandler socket