# Using the Sockets Library

**URL:** <https://discourse.julialang.org/t/using-the-sockets-library/60448>\
**Category:** General Usage\
**Created:** [May 3, 2021, 5:57am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448 "2021-05-03T05:57:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 3, 2021, 5:57am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/1 "2021-05-03T05:57:28Z")

</div>

Hello, i have been having trouble with the sockets library.  
For some reason when i write to a connection it doesnt succeed and instead hangs. I have no idea why this is happening, would anyone else have a clue?

```julia
function start(port)
        server = Sockets.listen(port)
        while true
          conn = Sockets.accept(server)
          @async begin
            println("Connection Start")
            processConnection(conn)
          end
        end
    end
    function processConnection(conn)
        data = Vector{UInt8}()
        try
            while true
                tmp = read(conn)
                if(length(tmp) > 0)
                    append!(data,tmp)
                else
                    break
                end
                println("here")
            end
        catch err
            print("connection ended with error $err")
        end
        println(data)
        if(length(data) == 0)
            return 
        end
        println(ntoh(data[1]), " ",data[1])
        transit_data = Transit.decode(data)
        buffer = processTransitData(transit_data)
        println("ABOUT TO WRITE")
        write(conn,buffer)
        println(conn," ",buffer)
        close(conn)
    end

```

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 4, 2021, 8:04am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/2 "2021-05-04T08:04:22Z")

</div>

After some more testing it appears to be that the “write” to the socket doesnt actually write until the program ends on its own. I have no clue why this may be.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 4, 2021, 10:49am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/3 "2021-05-04T10:49:05Z")

</div>

`read` blocks. The example from [Sockets Library issue: write delays until program ends · Issue #40705 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/40705) works as you expect if you use something like this instead:

Server:

```julia
using Sockets
server = Sockets.listen(8080)
conn = Sockets.accept(server)
while !eof(conn)
    data = readavailable(conn) # <-- !!!
    println(data)
    write(conn, data)
end

```

Client:

```julia
using Sockets
client = Sockets.connect(8080)
write(client, "hello")
println(readavailable(client)) # <-- !!!

```

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 4, 2021, 11:05am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/4 "2021-05-04T11:05:21Z")

</div>

Thank you, i felt like it was something i was doing wrong

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 4, 2021, 11:17am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/5 "2021-05-04T11:17:26Z")

</div>

Actually i seem to be having a different issue now. For some reason, readavailable() is blocking when there is no data available.  
Any reason why that may occur?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 5, 2021, 10:26pm UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/6 "2021-05-05T22:26:10Z")

</div>

The documentation is a bit ambiguous but that seems to be the intended behavior: read whatever data is available but block until _some_ data is available. Perhaps @jameson can comment on whether that’s what was intended (IIRC, he wrote this code originally).

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [May 6, 2021, 2:05am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/7 "2021-05-06T02:05:52Z")

</div>

I’ve been using `readavailable()` for years and it has always blocked. The way I use it is in an async task with a channel for communication.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 6, 2021, 3:01am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/8 "2021-05-06T03:01:42Z")

</div>

Probably not me: I usually tell people that `readavailable` is a foot-gun, and not to use. And if you think you need it, you should go back through the design and and check for accidental data handling errors (e.g. races and deadlocks). The error here with OP is forgetting to close the `write` side after being done with it (that said, we should someday finally implement half-open / shutdown state sockets–but they aren’t that commonly used since they’re often redundant with high-level protocols). Currently I think you have to ccall `uv_shutdown` manually now, which involves a bit of boilerplate unfortunately.

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 6, 2021, 3:04am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/9 "2021-05-06T03:04:38Z")

</div>

Hello, the problem was that i needed to wait for a reply, but the original write never ended up being sent until the socket closed. I thought TCP sockets were meant to just send when written too? Or do they require a “send” after writing to the socket first? Sorry im not the best with TCP sockets

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 6, 2021, 3:16am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/10 "2021-05-06T03:16:02Z")

</div>

You do seem to have a convoluted `processConnection` function with a lot of redundancy. Why not shorten it to the (functionally equivalent) form below:

```julia
    function processConnection(conn)
        data = read(conn)
        println(data)
        if isempty(data)
            return 
        end
        ...

```

Sockets do require a “send” call, known as “shutdown” or “half-close” if you are not using a higher level protocol. This is the `ccall(:uv_shutdown)` call that I mentioned above is not currently available.

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 6, 2021, 4:43am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/11 "2021-05-06T04:43:52Z")

</div>

Thanks for the improvement, ill add it in right away.  
But what would happen if a large amount of data was sent in multiple packets? Couldnt it read while stuff is still being sent and miss the end of the message?  
Also are you saying i should be calling ccall(:uv\_shutdown) after my write code? Or is it fine with the higher level library?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 6, 2021, 1:34pm UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/12 "2021-05-06T13:34:33Z")

</div>

TCP does not expose the concept of packets. You can either use a higher level protocol (like HTTP) or call uv\_shutdown after your write. Unfortunately, though uv\_shutdown isn’t quite trivial to wrap.

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 9, 2021, 3:24am UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/13 "2021-05-09T03:24:21Z")

</div>

Where would i find information on that uv\_shutdown command? im looking for what its missing and cant seem to find it.  
Also, if i want to write to the socket again would i need to do some form of “startup” command? or is that unneeded?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 11, 2021, 4:32pm UTC](https://discourse.julialang.org/t/using-the-sockets-library/60448/14 "2021-05-11T16:32:58Z")

</div>

It doesn’t really exist now, so you would need to use an existing library that also provides packet framing (ZMQ.jl, Protobuf.jl, HTTP.jl, Serialization.jl, etc). I wrote the code for supporting it just now: [add stream shutdown and support half-duplex operation by vtjnash · Pull Request #40783 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/40783)
