How do I get an entire response from a socket?

Posted on 2008-11-18
Last Modified: 2013-11-13
We are using the .net class for some ftp functionality.

After sending a command on an open socket we call the socket.receive (buffer).  This puts a certain number of bytes into the buffer and returns the number of bytes.

The problem is that sometimes the operation returns 0 but we have not received the entire response.   The next call to the receive method will then return the remaining message from this command PLUS the response generated by the subsequent command.
We suspect this happens due to network latency and varying ftp server loads etc.

My question is how can we reliably know when we have read back the entire outstanding response from the socket?
Question by:Paul_Harris_Fusion
    LVL 7

    Expert Comment

    When using the FTP protocol, you receive messages (Acknowledgments) that signal whether the server has finished uploading the file or not. You have to handle some sort of protocol.
    LVL 6

    Expert Comment

    If you know how much to read, insert that as buffer size even if your buffer is bigger to only read the remaining bytes.
    LVL 12

    Author Comment

    Juan - the problem is in knowing that I have received the entire message back from the server via the socket.

    Werrock - I am doing this already.

    The problem is that I submit a command and then call the Receive method on the socket with a buffer of e.g. 512 bytes and the method returns say 200 bytes.    Receiving less than the buffer size back normally indicates that all data has been received.   However,  when we submit the next command and issue another Receive,  the first part of the response is the remainder from the previous command.   The problem seems to be related to server speed.   Our only workaround to date is putting in thread sleeps to allow the server time to send its response but this obviously slows the component down.

    We have tried using the Available property on the .Net socket but this suffers from the same problem - if the server is slow to respond, the socket does not know that a response is coming.

    I was hoping that there was some reliable End-of-message marker that we could look for.
    LVL 12

    Accepted Solution

    Found it

    Problem was not with the socket but with my lack of knowledge of the FTP protocol.    
    FTP returns a Code and an optional comment in response to a command.   The separator between the code and the command is either a space or a hyphen.  The space indicates that the entire response has now been received whereas the hyphen indicates more is to come.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now