# Check host port availability

**URL:** <https://discourse.julialang.org/t/check-host-port-availability/44008>\
**Category:** New to Julia\
**Created:** [July 30, 2020, 8:59pm UTC](https://discourse.julialang.org/t/check-host-port-availability/44008 "2020-07-30T20:59:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [July 30, 2020, 8:59pm UTC](https://discourse.julialang.org/t/check-host-port-availability/44008/1 "2020-07-30T20:59:44Z")

</div>

Using `Sockets`, the `connect` function serves to establish a connection through a TCP socket

```julia
connect([host], port::Integer) -> TCPSocket

```

only problem is, if the host is not listening through this port, trying to connect will result in an error

```julia
julia> connect(5000)
ERROR: IOError: connect: connection refused (ECONNREFUSED)
Stacktrace:
 [1] wait_connected(::TCPSocket) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Sockets\src\Sockets.jl:520
 [2] connect at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Sockets\src\Sockets.jl:555 [inlined]
 [3] connect at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Sockets\src\Sockets.jl:541 [inlined]
 [4] connect(::Int64) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Sockets\src\Sockets.jl:537
 [5] top-level scope at REPL[2]:100:

```

Is there any form to check if the port is available before trying to connect? More specifically, is there a way (appart from a while-try-catch-loop) for the client to wait for the availability of the host? Simmilarly to how `accept(listen(5000))` waits for a client to connect.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 31, 2020, 7:26pm UTC](https://discourse.julialang.org/t/check-host-port-availability/44008/2 "2020-07-31T19:26:37Z")

</div>

I think try/catch is your only option, even if there was a way to check availability, without some sort of atomic “check/listen” or “check/reserve” operation there would be a race condition between checking availability and actually listening, so you would always have to handle the listen throwing an exception.
