# How to read from socket in non-blocking mode

**URL:** https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710
**Category:** New to Julia
**Created:** [July 7, 2017, 8:30am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710 "2017-07-07T08:30:11Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![DRVTiny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drvtiny/32/1292_2.png) [@DRVTiny](https://discourse.julialang.org/u/DRVTiny)
#### Post date: [July 7, 2017, 8:30am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/1 "2017-07-07T08:30:11Z")

</div>

I was trying to skip some initial server info header when using tcp client in Julia 0.6.0:

```julia
julia> s=connect("smtp.mail.ru",25)
TCPSocket(RawFD(18) open, 0 bytes waiting)

julia> nb_available(s)
0

julia> nb_available(s)
0
(after 5 seconds or so...)
julia> nb_available(s)
0

julia> t=read(s,10)
10-element Array{UInt8,1}:
 0x32
 0x32
 0x30
 0x20
 0x73
 0x6d
 0x74
 0x70
 0x31
 0x34
(HOW, WHY???? nb_available==0, but read returns me 10 bytes?!)

... (read was repeated many times...)
julia> t=read(s,10)
^CERROR: InterruptException:
Stacktrace:
 [1] process_events at ./libuv.jl:82 [inlined]
 [2] wait() at ./event.jl:216
 [3] wait(::Condition) at ./event.jl:27
 [4] wait_readnb(::TCPSocket, ::Int64) at ./stream.jl:296
 [5] readbytes!(::TCPSocket, ::Array{UInt8,1}, ::Int64) at ./stream.jl:714
 [6] read(::TCPSocket, ::Int64) at ./io.jl:529

```

So… nb\_available shows something incorrect. Mail server sends me some initial header, but nb\_available cant see it and tell me that 0 bytes awaiting in the read buffer. Why?  
Is there any other posobilities to skip server “greatings” header without blocking?

Thanks!

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [July 7, 2017, 8:37am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/2 "2017-07-07T08:37:48Z")

</div>

`nb_available` only tells you whether some bytes can be read _without blocking_. `read` can return more data , but then it may block (as `?read` says).

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [July 7, 2017, 9:16am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/3 "2017-07-07T09:16:44Z")

</div>

All I/O in julia is non-blocking in the traditional sense of the word, because we don’t block threads on I/O activity. However, we do use tasks to simulate blocking I/O for ease of programming. The `nb_available` function isn’t super meaningful for sockets. It tells you how many bytes are available in the userspace buffer. It says nothing about now many bytes are available in the kernel, in various buffers along the network path or are in transit.

---

<div class="post-metadata">

### Author: ![DRVTiny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drvtiny/32/1292_2.png) [@DRVTiny](https://discourse.julialang.org/u/DRVTiny)
#### Post date: [July 7, 2017, 11:38am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/4 "2017-07-07T11:38:22Z")

</div>

But why nb\_available == 0 and right after this - i can read more than 100 bytes without blocking? This is very strange, i think

---

<div class="post-metadata">

### Author: ![DRVTiny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drvtiny/32/1292_2.png) [@DRVTiny](https://discourse.julialang.org/u/DRVTiny)
#### Post date: [July 7, 2017, 11:43am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/5 "2017-07-07T11:43:49Z")

</div>

Thanks 🙂

But… Anyway, how to read initial bytes which server may send to me/may not send - and avoid the risk to be blocked on read command?

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [July 7, 2017, 12:55pm UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/6 "2017-07-07T12:55:46Z")

</div>

You do all your processing in a task.

```julia
@async begin
while !eof(socket)
line = readline(socket)
# process line
end
end

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [August 12, 2017, 9:52pm UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/7 "2017-08-12T21:52:27Z")

</div>

What if we want to work interactively, meaning that I do not want to process the returned data in the task? Is there a way to have a timeout?

The purpose here is e.g. working with SCPI.

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [August 14, 2017, 8:10pm UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/8 "2017-08-14T20:10:47Z")

</div>

Friendly bump here, timeouts are quite necessary when working with instruments.

---

<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: [August 14, 2017, 10:07pm UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/9 "2017-08-14T22:07:57Z")

</div>

```julia
@async (sleep(timeout); close(socket))

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [August 15, 2017, 12:43am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/10 "2017-08-15T00:43:40Z")

</div>

If I’m not mistaken, this will need a whole new socket to be created for the next message after the timeout, right?

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [August 15, 2017, 12:46am UTC](https://discourse.julialang.org/t/how-to-read-from-socket-in-non-blocking-mode/4710/11 "2017-08-15T00:46:44Z")

</div>

Also, if the read does complete, the socket will still get closed.
