Inspect buffer on open TCP socket

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?

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

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

    @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.

Maybe the function nb_available() (e.g. nb_available(cs) )?

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.

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.

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