1

My code is something like this:

connectFarEnc ip port = withSocketsDo $ do
  addrinfo <- getAddrInfo (Just (defaultHints {addrFlags = [AI_PASSIVE]})) ip (Just port)
  let addr = head addrinfo          
  sock <- socket (addrFamily addr) Stream defaultProtocol 
  connected <- timeout 2000000 $ connect sock (addrAddress addr)
  print $ show connected
  return (sock, connected)

I expect timeout of two second if cannot connect to the server, but it doesn't work. Instead, throw out an exception of connection failed, and quit the program.

How should I deal with this situation to avoid quit?

4

1 回答 1

1

你从服务器得到了一个明确的答案,它产生了一个“连接被拒绝”。超时适用于您根本没有得到答案的情况。

于 2013-02-26T22:32:54.670 回答