Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data NetException = NetNoException | NetTimeOut | NetRefused
- deriving (Show, Eq)
- diffExcept :: Either SomeException Handle -> Either NetException Handle
- diffExcept (Right h) = Right h
- diffExcept (Left (SomeException m))
- | err == "WSAETIMEDOUT" = Left NetTimeOut
- | err == "WSAECONNREFUSED" = Left NetRefused
- | otherwise = error $ show m
- where
- err = reverse . dropWhile (== ')') . reverse . dropWhile (/='W') $ show m
- main :: IO ()
- main = do
- withSocketsDo $ do
- putStr "Host?\n\t"
- host <- getLine; putStrLn ""
- putStr "Port?\n\t"
- p <- getLine; putStrLn ""
- let port = read p :: Int
- good <- testAddress host port
- print good
- main
- testAddress :: HostName -> Int -> IO (Maybe Handle)
- testAddress host iPort = do
- let port = PortNumber $ fromIntegral iPort
- putStrLn $ "Testing - Host: " ++ host ++ ", Port: " ++ show iPort
- result <- try $ connectTo host port
- let result' = diffExcept result
- case result' of
- Left NetTimeOut -> do print "Connection Timed Out"; return Nothing
- Left NetRefused -> do print "Connection Refused"; return Nothing
- Right h -> do
- putStrLn $ "Connected to " ++ host
- return $ Just h
Advertisement
Add Comment
Please, Sign In to add comment