port-utils

Utilities for creating and waiting on ports

https://github.com/jfischoff/port-utils#readme

Version on this page:0.2.0.0
LTS Haskell 22.14:0.2.1.0
Stackage Nightly 2024-03-28:0.2.1.0
Latest on Hackage:0.2.1.0

See all snapshots port-utils appears in

BSD-3-Clause licensed by Jonathan Fischoff
Maintained by [email protected]
This version can be pinned in stack with:port-utils-0.2.0.0@sha256:f19c6c80acf9964b8248d7a74c2cfc4a2d4cec40d272cec583638dbea585e104,1931

Module documentation for 0.2.0.0

  • Network
    • Network.Socket
      • Network.Socket.Free
      • Network.Socket.Wait
        • Network.Socket.Wait.Internal
Depends on 2 packages(full list with versions):
Used by 1 package in nightly-2019-05-10(full list with versions):

port-utils

openFreePort

This is another version of warp’s openFreePort function. This function has fewer dependencies than the warp version.

openFreePort returns a socket on a random port and the port it has been bound to.

openFreePort :: IO (Int, Socket)

wait

wait will attempt to connect to a host and port until it is successful. Between each unsuccessful attempt, it sleeps for 10 ms.

Here is an example of the primary function:

import Network.Socket.Wait (wait)

void $ forkIO $ Warp.run 7000 app
-- Wait for the server to start listening on the socket
wait "127.0.0.1" 7000
-- Communicate with the server
...

In bash one could write:

while ! nc -z localhost 7000 ; do sleep 0.01 ; done

The bash script above was copied from this stackoverflow answer https://stackoverflow.com/a/50008755

Changes

Changelog for wait-on-port

  • 0.0.0.1 First version

  • 0.2.0.0 Rework internals and change EventHandlers to EventHandlers m. Fix bug if host could not be found. It would loop forever and now throws.