# Inspect buffer on open TCP socket

**URL:** <https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569>\
**Category:** General Usage\
**Created:** [November 25, 2016, 11:49pm UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569 "2016-11-25T23:49:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [November 25, 2016, 11:49pm UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/1 "2016-11-25T23:49:52Z")

</div>

Interaction with a TCP socket is mostly straightforward, however occasionally I find that the buffer is not empty on first opening, so the function starts processing the buffer when it should just wait for the buffer to start filling. I’d like to detect a non-empty buffer so that it can be cleared - how to accomplish?

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 26, 2016, 12:04am UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/2 "2016-11-26T00:04:25Z")

</div>

Can you provide some code of how you open the socket and assign the processing function?

---

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [November 26, 2016, 8:38am UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/3 "2016-11-26T08:38:36Z")

</div>

Here is the code, inside a function, with the server running:

```julia
    @spawn jinfo = readstring(`julius -C al.mod.jconf`)
    sleep(2)
    cs = ""
    try
        cs = connect(port)
    catch
        error("Shutdown: no server available")
    end

```

Followed by a call such as:  
`response = access_server(cs)`  
which contains a line such as:  
`buf = strip(readline(cs))`  
which is mostly empty but occasionally non-empty.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 26, 2016, 11:55am UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/4 "2016-11-26T11:55:36Z")

</div>

Maybe the function [nb\_available()](http://docs.julialang.org/en/release-0.5/stdlib/io-network/#Base.nb_available) (e.g. `nb_available(cs)` )?

---

<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:** [November 29, 2016, 5:09pm UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/5 "2016-11-29T17:09:27Z")

</div>

Can you provide an example of what kind of data is being found in the buffer? Nothing should be getting read into the buffer (nb\_available should return 0) until you do some sort of read operation on the socket (such as calling any of the read\* functions or eof, for example). But `readline` will always read the next line that gets transmitted, blocking if necessary. Thus, it should only be empty if the server sent an empty line.

---

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [November 29, 2016, 5:51pm UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/6 "2016-11-29T17:51:30Z")

</div>

The server, Julius, is a speech recognition application. Its job is to pick up microphone input and decode it into text.  
Since my initial post I have not seen anything odd in the buffer despite watching for it quite carefully. I did do some code reorganization which may explain this apparent resolution of what I saw. I may also have changed some server settings that make spurious sounds less detectable.  
I will report back if I see anything else meaningful.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [November 29, 2016, 6:25pm UTC](https://discourse.julialang.org/t/inspect-buffer-on-open-tcp-socket/569/7 "2016-11-29T18:25:33Z")

</div>

> [@Colin\_Beckingham](#):
>
> I may also have changed some server settings that make spurious sounds less detectable. I will report back if I see anything else meaningful.

If you see other unexpected behavior, use wireshark or tcpmon to check whether data is actually being sent from the server.
