BeginReceive return zero length buffer when run ,and work correctly when use step by step debug mode

  • Thread starter Thread starter Daniel
  • Start date Start date
I hope somebody is still reading this..
I have a similar problem and i'm not sure what to do. Let me start off
by saying that this is my first dive into the world of sockets.
I got some free c sharp code for a simple ftpclient.
I've been playing around with it some, trying to write a function to
return the size of a given directory on the ftp server. I think the
issue i ran into is similar to what's been discussed here but not
exactly.
In my scenario i use the straight Receive to get the data from the ftp
server and here's what i noticed. When i run the code with some
console.writeline's or step through it in a debugger it runs just
fine.
As soon as i remove the console.writeline's it fails - the size of data
received is not what its supposed be. Here is the code that executes
when this happens:

socket is created like this:


socket = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
ep = new IPEndPoint(Dns.Resolve(ipAddress).AddressList[0], port);
socket.Connect(ep);

and then used like this:


DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);

while( timeout > DateTime.Now )
{
int bytes = cSocket.Receive(buffer, buffer.Length, 0);

//if i put some Console.Writeline here it will work
otherwise it fails.


this.message += ASCII.GetString(buffer, 0, bytes);

if ( bytes < this.buffer.Length) break;
}

I'm assuming i'm experiencing the same problem ie the peer socket
closing before it's read

Please advise me on how to resolve this.
 
Hi Martin
how it's fail ?
throw exception or , return 0 bytes ?
if it's throw exception let's try to read it
if it's return zero bytes - based on my problem and the nice people help me
here - it's mean that the second party shutdown the connection
 
Its seems to be the latter. I dont get errors just no results returned.
The machine i connect to is our company ftp server so i dont understand
why it would be closing the socket. Anyway the socket's Connected
property returns true. I really dont know what to do with it.
 
Back
Top