Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,536 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,263 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



FTP RAW Commands problem

 
Reply to this topicStart new topic

FTP RAW Commands problem

nbarten
post 29 Jul, 2008 - 10:49 AM
Post #1


D.I.C Head

**
Joined: 30 Apr, 2007
Posts: 112



Thanked 1 times
My Contributions


hi all.

i'm stuck with my ftp client in development blink.gif

I made a succesful connection using a socket:

CODE

IPHostEntry ipHostInfo = Dns.Resolve(ftpaddress);
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 21);

                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp );

                // Connect the socket to the remote endpoint. Catch any errors.
                try {
                    sender.Connect(remoteEP);


After that i sent my username and password using the USER and PASS commands. All worked fine (after each command i sent i receive the answer from the ftp server).

After that i sent "TYPE A" for ASCII mode, and "PASV" for a passive connection. I received the passive ip address and the port, which i used to create a new socket connection.

After that, to receive the list of directories and files in the current directory, i sent the command "LIST" to the ftp server. I'm trying to receive an answer, but nothing comes.

Tried also "NLST" instead of "LIST" , but the same problem. Also, at the first socket connection (before i gave my username and password) i got a message from the ftp server that it was succesfully connected. At the second socket connection (after the PASV command) i didn't get that message (maybe something went wrong there already?).


I really don't see what i'm doing wrong.

Also, if somebody knows a good site with explanations en connection examples with the RAW Commands, please tell me.

This post has been edited by nbarten: 29 Jul, 2008 - 10:50 AM
User is offlineProfile CardPM

Go to the top of the page


nbarten
post 31 Jul, 2008 - 04:04 AM
Post #2


D.I.C Head

**
Joined: 30 Apr, 2007
Posts: 112



Thanked 1 times
My Contributions


No one? I've still no succes, looked already at Psychocoder's ftp code example, but i don't doing anything wrong...

a simple LIST command (with a Environment.Newline behind it) would be enough wouldn't it? But i get no response from the ftp server... crazy.gif

here's most of my code in Main:

CODE
static void Main(string[] args)
        {
            // Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            // Connect to a remote device.
            try {
                // Establish the remote endpoint for the socket.
                
                IPHostEntry ipHostInfo = Dns.Resolve("*********.com");
                IPAddress ipAddress = ipHostInfo.AddressList[0];
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 21);

                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp );

                // Connect the socket to the remote endpoint. Catch any errors.
                try {
                    sender.Connect(remoteEP); // verbinding maken met ftp server

                    Console.WriteLine("Socket connected to {0}",
                        sender.RemoteEndPoint.ToString());
                    
                    // get ftp server connection response
                    int bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    // 220
                    
                    // send username
                    byte[] msg = Encoding.ASCII.GetBytes("USER *******\n");
                    int bytesSent = sender.Send(msg); // username versturen naar ftp server
                    
                    // get response username (password needed)
                    bytesRec = sender.Receive(bytes); // ontvangen response (pass needed)
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    // 331

                    // send password
                    msg = Encoding.ASCII.GetBytes("PASS *******\n"); // password versturen naar ftp server
                    bytesSent = sender.Send(msg);

                    // get response from ftp server
                    bytesRec = sender.Receive(bytes); // ontvangen response (login succesvol)
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    // 230

                    // check directory currently in
                    msg = Encoding.ASCII.GetBytes("PWD\n");
                    bytesSent = sender.Send(msg);

                    bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));

                    // change directory
                    msg = Encoding.ASCII.GetBytes("CWD /***********\n");
                    bytesSent = sender.Send(msg);

                    bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));

                    // check directory again
                    msg = Encoding.ASCII.GetBytes("PWD\n");
                    bytesSent = sender.Send(msg);

                    bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));

                    // set type to ascii
                    msg = Encoding.ASCII.GetBytes("TYPE A\n");
                    bytesSent = sender.Send(msg);
                    
                    bytesRec = sender.Receive(bytes);
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));

                    // set to passive mode connection
                    msg = Encoding.ASCII.GetBytes("PASV\n");
                    bytesSent = sender.Send(msg);

                    // receive passive ip + port
                    bytesRec = sender.Receive(bytes);
                    String answer = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    Console.WriteLine("Response ftp: " + answer);

                    // set ip and port in variables
                    answer = answer.Substring(answer.LastIndexOf('(')+1, answer.LastIndexOf(')') - answer.LastIndexOf('(') -1);
                    Console.WriteLine("answer: " + answer);

                    String[] splittedString = answer.Split(new char[] { ',' });
                    String ipaddress = splittedString[0] + "." + splittedString[1] + "." + splittedString[2] + "." + splittedString[3];

                    int port = (int.Parse(splittedString[4]) << 8) + int.Parse(splittedString[5]);
                    Console.WriteLine("Address: " + ipaddress + ":" + port.ToString());
                    
                    // make new endpoint with ip and new port of passive ftp
                    IPEndPoint ipEndPoint = new IPEndPoint(Dns.GetHostEntry(ipaddress).AddressList[0], port);

                    // Create a TCP/IP  socket.
                    Socket transfersocket = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp );

                    // connect to passive ftp server with new socket                    
                    transfersocket.Connect(ipEndPoint); // verbinding maken met ftp server

                    // it shows the writeline in the console
                    Console.WriteLine("Socket connected to {0}",
                    transfersocket.RemoteEndPoint.ToString());
                    
                    // BUT (!!) i don't get response here of succesful connection
                    bytesRec = transfersocket.Receive(bytes); // response ontvangen of de verbinding goed tot stand is gebracht
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                        
                    
                    
                    /*
                    
                    msg = Encoding.ASCII.GetBytes("LIST -aL\n");                    
                    bytesSent = transfersocket.Send(msg);                    

                    bytesRec = transfersocket.Receive(bytes);                    
                    Console.WriteLine("Response ftp: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    
                    */
                    Console.Read();
                    // Release the socket.
                    sender.Shutdown(SocketShutdown.Both);
                    sender.Close();
                
                } catch (ArgumentNullException ane) {
                    Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
                } catch (SocketException se) {
                    Console.WriteLine("SocketException : {0}",se.ToString());
                } catch (Exception e) {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }

            } catch (Exception e) {
            Console.WriteLine( e.ToString());
            }

        }


passwords, usernames and such are now '*' of course.


Hope someone can help. After the new socketconnection to the passive ftp server, i don't get response from the ftp server.

This post has been edited by nbarten: 31 Jul, 2008 - 04:28 AM
User is offlineProfile CardPM

Go to the top of the page

nbarten
post 2 Aug, 2008 - 10:28 AM
Post #3


D.I.C Head

**
Joined: 30 Apr, 2007
Posts: 112



Thanked 1 times
My Contributions


com' on now. Isn't there someone on this forum who made a ftp client using raw ftp commands like USER, PASS and LIST?
User is offlineProfile CardPM

Go to the top of the page

nbarten
post 15 Aug, 2008 - 05:43 AM
Post #4


D.I.C Head

**
Joined: 30 Apr, 2007
Posts: 112



Thanked 1 times
My Contributions


well i give up on this project. Still haven't an answer sad.gif
User is offlineProfile CardPM

Go to the top of the page

nbarten
post 15 Aug, 2008 - 11:44 AM
Post #5


D.I.C Head

**
Joined: 30 Apr, 2007
Posts: 112



Thanked 1 times
My Contributions



biggrin.gif biggrin.gif biggrin.gif

Maybe i'm lucky today, although i'm a bit sick. Just surfed a bit over the internet, where i found a website how it should work a bit.

Data, like files and such, goes through another socket. And so does the return of the LIST command.

so, basically it is:

1) Send through socket 1 the PASV command (after the other needed commands)
2) Retrieve through socket 1 the ip address with the given port
3) Create a new socket (socket 2) and connect with IPEndpoint with the new ip and port given by the ftp server.
4) Send through socket 1 (not 2) the command LIST.
5) Listen on socket 2, you'll receive the list wink2.gif

Finally rolleyes.gif well decided to continue my little project.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 09:45PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month