我在窗口上用相同的代码问了一个关于信号的问题,这是另一个问题
import IO
import Control.Exception hiding (catch)
import Control.Concurrent
import Network
main = withSocketsDo $ do {main'}
main' = listenOn (PortNumber 9900) >>= acceptConnections
acceptConnections sock = do
putStrLn $ "trying to accept" ++ (show sock)-- debug msg
conn@(h,host,port) <- accept sock
print conn -- debug msg
forkIO $ catch (talk conn `finally` hClose h) (\e -> print e)
acceptConnections sock
talk conn@(h,_,_) = hGetLine h >>= hPutStrLn h >> hFlush h >> talk conn
我在win7上运行程序,看起来套接字创建成功,但是我无法telnet on,netstat没有显示任何监听套接字的进程,有什么问题吗?或者,haskell有bug在Windows上?(顺便说一句,在这个代码的debian上工作完美)
答案 0 :(得分:1)
当我在win7上使用netstat -a时,我发现端口9900的侦听ip是[::],127.0.0.1和0.0.0.0都没有,所以我猜问题是在函数“listenOn”的创建套接字期间,然后我写了“listenOn2”替换。问题解决了。 这是完整的代码:(唯一的区别是将[[getProtocolNumber“tcp”]]的原型更改为[[defaultProtocol]]),也许这是一个错误。
import IO
import Control.Exception hiding (catch)
import Control.Concurrent
import Network
import Network.Socket
listenOn2 (PortNumber port) = do
--proto <- getProtocolNumber "tcp" ,here is the difference!!!!
let proto = defaultProtocol
bracketOnError
(socket AF_INET Stream proto)
(sClose)
(\sock -> do
setSocketOption sock ReuseAddr 1
--addr <- inet_addr "127.0.0.1"
bindSocket sock (SockAddrInet port iNADDR_ANY)
listen sock maxListenQueue
return sock
)
main = withSocketsDo $ do {main'}
main' = listenOn2 (PortNumber 9900) >>= acceptConnections
acceptConnections sock = do
putStrLn $ "trying to accept" ++ (show sock)-- debug msg
conn@(h,host,port) <- Network.accept sock
print conn -- debug msg
forkIO $ catch (talk conn `finally` hClose h) (\e -> print e)
acceptConnections sock
talk conn@(h,_,_) = hGetLine h >>= hPutStrLn h >> hFlush h >> talk conn