# Network socket appears open, but then immediately closes

**URL:** https://discourse.julialang.org/t/network-socket-appears-open-but-then-immediately-closes/128481
**Category:** General Usage
**Tags:** network
**Created:** [April 28, 2025, 8:05am UTC](https://discourse.julialang.org/t/network-socket-appears-open-but-then-immediately-closes/128481 "2025-04-28T08:05:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [April 28, 2025, 8:05am UTC](https://discourse.julialang.org/t/network-socket-appears-open-but-then-immediately-closes/128481/1 "2025-04-28T08:05:18Z")

</div>

I’m writing a program which connects to a similar program on another computer; right now my desktop connects to my laptop. Both have IPv4 and IPv6 addresses; the IPv4 address is local-only, while the IPv6 address is globally visible but occasionally changes because of the weather. The program tries all the addresses it gets from `getalladdrinfo` until it gets a successful connection, then passes the connection to a task, which sends and receives messages on the connection. Within the past hour or two, the IPv6 addresses have changed. The program keeps saying “closed” and transferring nothing. Having recently changed something in `connectionTask`, I investigated but found out that what I changed was not the culprit, it was being passed a closed socket. Here are some `@info` outputs from the latest run:

```julia
julia> whichEar=64
┌ Info: dest
└ d = ip"2001:5b0:2172:b3f8:b32:42db:86ab:352f"
┌ Info: connectTask
│ dest =
│ 2-element Vector{Sockets.IPAddr}:
│ ip"2001:5b0:2172:b3f8:b32:42db:86ab:352f"
│ ip"192.168.42.201"
└ sock = Sockets.TCPSocket(RawFD(25) open, 0 bytes waiting)
┌ Info: connectionTask
└ conn = EANetwork.Connection(Sockets.TCPSocket(RawFD(-1) closed, 0 bytes waiting), UInt8[], UInt8[], 0, -1, 0.5784105585570988, 0x003d51d05cd98be6, 0x003d51d0349fe157, true, false, false, Message(<snip>), Message(<snip>))
closed

```

`whichear=64` is output by the program and means that it’s listening on only an IPv6 socket; I’m running Linux and IPv6 sockets can handle IPv4 connections as well. If I were running this on BSD, `whichear` would be 80, if what I remember from trying to program network sockets over a decade ago still applies.

The snipped stuff is an empty message. What’s in the `Message` struct is irrelevant to the question.

The times indicate that 675 ms elapsed between creating the `Connection` with the socket in it and the last activity, which appears to have been saying hello.

Should I wait a second before checking whether the socket is open, or how should I check?

---

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [April 28, 2025, 9:08am UTC](https://discourse.julialang.org/t/network-socket-appears-open-but-then-immediately-closes/128481/2 "2025-04-28T09:08:24Z")

</div>

The solution is to send a character immediately after opening the socket. If there’s nothing at the address, it results in this error:

```julia
ERROR: IOError: write: connection reset by peer (ECONNRESET)

```

which I catch and try the next address.
