?
Solved

Ftp client to list remote directory

Posted on 2005-05-12
6
Medium Priority
?
382 Views
Last Modified: 2010-04-05
Hello,

It is my first application with socket.

I try to access remote directory via FTP and then liste the content.

I can connect to the ftp server .
Then send a user command and Pass command
I also change the current directory

Now I'm not sure :

I send a port command and then a list command.
The receiver socket receive nothing ...

It semms to block at this line >ListeningSoc := Datasoc.Accept;

Here is my code :

 lmessage := arraylist.Create;

  SetLength(buffer,buffersize);

  RemotePoint := ipendpoint.create( dns.Resolve('x.x.x.x').addresslist[0],21);
  clientsocket := socket.create(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

  clientsocket.Connect(RemotePoint);

   intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
 //  while intreceive>0 do
 //  begin
 //  intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);
 //  end;

  clientsocket.Send( ASCIIEncoding.ASCII.GetBytes('USER a'+ Environment.NewLine));

  intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);

  clientsocket.Send( ASCIIEncoding.ASCII.GetBytes('PASS b'+ Environment.NewLine));

  intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);

  clientsocket.Send( ASCIIEncoding.ASCII.GetBytes('CWD 10558'+ Environment.NewLine));

  intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);

  //receiver socket
  datasoc := socket.Create(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  Host := Dns.Resolve(Dns.GetHostName());
  localPoint :=  IPEndPoint.create(Host.AddressList[0], 0);

  Datasoc.Bind(localpoint);
  Datasoc.Listen(5);
 
  //get the listen port
   ListenPort:=  IPEndPoint( Datasoc.LocalEndPoint).Port;
   address := ipendpoint(datasoc.LocalEndPoint).Address;

   //Set the port on the server
    hostbyte := BitConverter.GetBytes(ipendpoint(datasoc.LocalEndPoint).Address.Address);
    portbyte := BitConverter.GetBytes( IPEndPoint( Datasoc.LocalEndPoint).Port);

     cmd := StringBuilder.Create('PORT ')
    .append( hostbyte[0]).Append(',')
    .append( hostbyte[1]).Append(',')
    .append( hostbyte[2]).Append(',')
        .append( hostbyte[3]).Append(',')
            .append( portbyte[0]).Append(',')
            .append( portbyte[1]).tostring;


  clientsocket.Send(ASCIIEncoding.ASCII.GetBytes(cmd+ environment.NewLine));
  intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);

  clientsocket.Send(ASCIIEncoding.ASCII.GetBytes('LIST' + environment.NewLine))   ;
   ListeningSoc := Datasoc.Accept;

  intReceive :=  clientsocket.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);

//  intReceive :=  Datasoc.Receive(buffer,buffersize,SocketFlags.None);

    ListeningSoc.Receive(buffer,buffersize,SocketFlags.None);
  lMSG.fMessage :=  ASCIIEncoding.ASCII.GetString(buffer);
  lMessage.Add(lmsg);

   DataGrid1.DataSource := lMessage;
   datagrid1.DataMember := 'msg';
   datagrid1.DataBind;


Thank you

0
Comment
Question by:PHD
  • 3
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
calinutz earned 1920 total points
ID: 13993686
Did you try the Indy way?
Something like...
place a idFTP component from the Indy Clients components tab, a listBox and a Button

After that , configure your idFTP with username ,password and remotehost
and then onClick Button place this code:

procedure TForm1.Button1Click(Sender: TObject);
var CurrentDir,Dirname,name:String;
    LS:TStringList;
    i:integer;
begin
   if IdFTP1.Connected then exit;
   idftp1.Connect();
   try
    LS:=TStringList.Create;
    DirName:='/root';
   IdFTP1.ChangeDir(DirName);
   IdFTP1.TransferType:=ftASCII;
   CurrentDir:=IdFTP1.RetrieveCurrentDir;
   Listbox1.Items.Clear;
   IdFTP1.List(LS);
    for i:=0 to IdFTP1.DirectoryListing.Count-1 do
  begin
      Name:=IdFTP1.DirectoryListing.Items[i].FileName;
      listbox1.Items.Add(Name);
  end;
  finally
  LS.Free;
  end;
end;
0
 
LVL 6

Author Comment

by:PHD
ID: 13993697
Thank you.

I use delphi 2005 and I think I don't have indy component for .Net framework.

0
 
LVL 11

Expert Comment

by:calinutz
ID: 13993705
You can also use this page in solving your problem. ICS is a very good place to start your socket applications:
http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html

Besides the components that are written in Object Pascal native VCL, you can find there sample applications from where you may get inspired.
Cheers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:PHD
ID: 13993763
it's look like a nice site.
I just downloaded the Internet Component Suite that include .net component.
I will test it this week-end.

Thanks
0
 
LVL 11

Expert Comment

by:calinutz
ID: 13993927
For .NET application that access FTP try this free components:
http://www.handyarchive.com/Development/Components-Libraries/9741-FTP-Component.html

But I think that ICS component Suite should work just fine for you. I did not try them because I only use Delphi 7 but from the description I assume that the ICS does not depend on Delphi's version.
Cheers
0
 
LVL 22

Assisted Solution

by:Mohammed Nasman
Mohammed Nasman earned 80 total points
ID: 14001770
Indy 10 supports .net

if you start VCL.Net application, you could see the indy components installed
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 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