• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

How do I get an entire response from a socket?

We are using the .net system.net.sockets.socket 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?
0
Paul_Harris_Fusion
Asked:
Paul_Harris_Fusion
  • 2
1 Solution
 
JuanCarnigliaCommented:
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.
0
 
WerrockCommented:
If you know how much to read, insert that as buffer size even if your buffer is bigger to only read the remaining bytes.
0
 
Paul_Harris_FusionAuthor Commented:
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.
0
 
Paul_Harris_FusionAuthor Commented:
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now