Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import Prelude hiding (catch)
  2. import System (getArgs)
  3. import System.IO (hPutStrLn, hSetBuffering, BufferMode(NoBuffering), hClose, Handle)
  4. import System.IO.Error (isEOFError, IOError(..))
  5. import Network (connectTo, PortID(..), withSocketsDo)
  6. import Control.Concurrent (forkIO, threadDelay)
  7. import Control.OldException
  8.  
  9. delay :: Int
  10. delay = 5
  11.  
  12. main :: IO ()
  13. main = withSocketsDo $ do
  14. [host, port] <- getArgs
  15. pinger host port
  16. return ()
  17.  
  18. pinger :: String -> String -> IO ()
  19. pinger host port = do
  20. handle <- connectTo host $ PortNumber $ fromIntegral (read port :: Int)
  21. start handle host port `catch` handler `finally` hClose handle
  22. where
  23. handler (IOException e)
  24. | isEOFError e = putStrLn "meowing"
  25. handler e = putStrLn $ show e
  26.  
  27. start :: Handle -> String -> String -> IO ()
  28. start handle host port = do
  29. putStrLn "Sending ping to Nexus"
  30. hSetBuffering handle NoBuffering
  31. hPutStrLn handle "ping"
  32. hClose handle
  33. threadDelay $ (floor $ fromIntegral delay*1e6 :: Int)
  34. pinger host port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement