Created
September 15, 2017 16:40
-
-
Save jfischoff/1cedd4a7cd874499f62447a7c21b143f to your computer and use it in GitHub Desktop.
Common wait for a socket function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
waitForServer :: Int -> IO () | |
waitForServer port = handle (\(_ :: IOException) -> waitForServer port) $ do | |
let hints = S.defaultHints { S.addrFlags = [ S.AI_NUMERICHOST | |
, S.AI_NUMERICSERV | |
] | |
, S.addrSocketType = S.Stream | |
} | |
addr:_ <- S.getAddrInfo (Just hints) (Just "127.0.0.1") (Just $ show port) | |
bracket (S.socket (S.addrFamily addr) (S.addrSocketType addr) (S.addrProtocol addr)) | |
S.close | |
$ \sock -> S.connect sock $ S.addrAddress addr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment