1
votes

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?

1
Where is the code for timeout coming from? Have you tried to catch the exception? We need more input. - Thomas M. DuBuisson
Hi, timeout is imported from System.Timeout. I just tried 'catch' and it can avoid quit, thanks. - Xingtao

1 Answers

1
votes

You got a definite answer from the server, which produced a 'connection refused'. The timeout is for when you get no answer at all.