Socket.Receive

J

Jason

Hi all

I have a little FTP proggie. It works, on windows xp, but it doesnt work
properly on windows 2000. The first problem i have, is when retrieving a
list of files.
int bytes = cSocket.Receive(buffer, buffer.Length, 0);

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

bytes is returned as size 18, my buffer.Length is set at 512, but i only get
18 back. i know for a fact that there is more since it works on my XP
machines. the above piece of code is in a while loop, keeps concatenating
the results to this.message.

so on the windows 2000 platforms i only get back the first file, whereas
with xp i get back the entire list. files are separated by "\r\n".

that is my first error.

the next error is when the cSocket.Receive just hangs. I think this is
because it is blocking itself until there is data to read. but i know there
is data to read, because it works on my XP machines. the programs "comes
back" after 10 mins, which is the connection idle timeout set on the FTP
server.

is there a component i need to install on the 2000 machines?

another peculiar note: I have tested it on four windows 2000 boxes. Two of
the four boxes "hang" for the 10min, where the other two of the four boxes
repond after the first few seconds. although they dont respond in the
desired way. but ALL four windows 2000 boxes only read back the first file.

windows xp works perfectly.

any suggestions are most welcome!

Thanks
Jason
 
J

Jon Skeet [C# MVP]

Jason said:
I have a little FTP proggie. It works, on windows xp, but it doesnt work
properly on windows 2000. The first problem i have, is when retrieving a
list of files.
int bytes = cSocket.Receive(buffer, buffer.Length, 0);

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

bytes is returned as size 18, my buffer.Length is set at 512, but i only get
18 back. i know for a fact that there is more since it works on my XP
machines. the above piece of code is in a while loop, keeps concatenating
the results to this.message.

Right. I don't see why that's a problem. Sockets don't guarantee to
return everything in one lump - just loop until you've got everything.
(Oh, and use a StringBuilder instead of string concatenation.)
so on the windows 2000 platforms i only get back the first file, whereas
with xp i get back the entire list. files are separated by "\r\n".

that is my first error.

the next error is when the cSocket.Receive just hangs. I think this is
because it is blocking itself until there is data to read. but i know there
is data to read, because it works on my XP machines.

I suggest you use a network analyser to confirm that, rather than just
XP machines. Perhaps the server is returning different information to
the 2000 boxes?
 
J

Jason

I have tried looping

while (cSocket.Available != 0) {
}

yeh, stringbuilder better, thanks. just need to get this working first hehe

so what would you do? i am very new at sockets. for example how would i
check if i have everything or not?

Thanks again
 
J

Jon Skeet [C# MVP]

Jason said:
I have tried looping

while (cSocket.Available != 0) {
}

I wouldn't do that - I'd just let the socket block. Just loop around
until you know that you've got everything.
yeh, stringbuilder better, thanks. just need to get this working first hehe

so what would you do? i am very new at sockets. for example how would i
check if i have everything or not?

The protocol should include appropriate data. There's no concept in a
network stream of "the end" until the connection is closed.

I can't remember exactly how FTP works, but I'm sure there'll be
something you can use to detect when you've finished receiving the
current file (or list of files, or whatever).
 
J

Jason

sigh, i thought this would work (from MSDN)

// Receive the host home page content and loop until all the data is
received.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}

but i also get "blocked"

yeh CRLF is "\r\n" which is what separates my file names...

Thanks
Jason

Nicholas Paldino said:
RFC 959 indicates that the end of line is indicated with a CRLF
sequence.

You can find RFC 959 at:

http://www.w3.org/Protocols/rfc959/

Hope this helps.
 
J

Jon Skeet [C# MVP]

Jason said:
sigh, i thought this would work (from MSDN)

// Receive the host home page content and loop until all the data is
received.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}

but i also get "blocked"

yeh CRLF is "\r\n" which is what separates my file names...

Yes, you would get blocked - because the server hasn't been told to
close the connection at the end of the request, presumably.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top