0
votes

I'm implementing a very simple FTP server program that is able to retrieve and store text files. my question is, when a ftp client request say a file with the following "RETR test.txt" how does the server send this file?. Does it open that text file copy the contents into a buffer and simply send it or is there more to it?. I'm not sure how to implement this, could someone clarify the basic idea?.

edit::

if (strncmp(receive_buffer,"RETR",4)==0)  {
                sprintf(send_buffer,"150 Opening ASCII mode data connection... \r\n");
                printf("<< DEBUG INFO. >>: REPLY sent to CLIENT: %s\n", send_buffer);
                bytes = send(ns, send_buffer, strlen(send_buffer), 0);
                if (bytes < 0) break;
                closesocket(ns);

                char temp_buffer2[80];

                FILE *fin=fopen("test.txt","r");//open test.txt
                while (!feof(fin)){
                    fgets(temp_buffer2,78,fin);
                    sprintf(send_buffer,"%s",temp_buffer2);
                    printf("%s",send_buffer);

                    if (active==0) {
                        printf("***active is 0");
                        send(ns_data, send_buffer, strlen(send_buffer), 0);
                    }
                    else {
                        printf("***active is 1+");
                        send(s_data_act, send_buffer, strlen(send_buffer), 0);
                    }
                }
                fclose(fin);
                sprintf(send_buffer,"226 File transfer complete. \r\n");
                printf("<< DEBUG INFO. >>: REPLY sent to CLIENT: %s\n", send_buffer);
                bytes = send(ns, send_buffer, strlen(send_buffer), 0);
                if (active==0 )closesocket(ns_data);
                else closesocket(s_data_act);
            }

this is what I have done, and for the RETR command it opens test.txt to try and send it. But this causes the server to disconnect..

2

2 Answers

2
votes

It doesnt matter how you send it. You can read one byte at a time and call Send for each byte. Or you could create a buffer of 8192 bytes and send that much at a time. Or any other number of bytes.

What do matter is if the FTP server is in active or passive mode: http://slacksite.com/other/ftp.html

1
votes

One bug in your code is that you are calling closesocket(ns) to disconnect the client's command connection after sending the intial reply, before sending the file over the data connection.