0

I would like to please you guys for an advice. I want to write FTP client in C/C++, I don't want to use some already written libraries, so I decided to write it all on my own. But I am facing a small problem right now.

During sending any data on server everything goes right, the file is created and filled with data, but the server is totally ignoring '\r\n' and '\n' symbols. E.g. I send some text file with many rows, but the server connects it to one line.

I have tried a lot, but I have no idea, where the problem could be. I already tried to send the file from my client to my temporary server (recv() function only) and there is the file saved correctly as it should. So i don't know why FTP servers have problem with that.

(I do everything in Winsock 2)

Here is the function for sending file from my client:

int PutFile(char *filename)
{
    fstream stream;
    stream.open(filename, ios::in | ios::binary);
    if(!stream.is_open())
    {
        Print("FTP_COMMAND[info]( %d ): Could not open file!", __LINE__);
        return -1;
    }
    int sizeoffile = 0;
    int filepos = 0;
    stream.seekg(0, ios::end);
    sizeoffile = stream.tellg();
    stream.seekg(0, ios::beg);

    if(sizeoffile <= 0)
    {
        Print("FTP_COMMAND[in]( %d ): Invalid file!", __LINE__);
        return -1;
    }

    char *file = new char[sizeoffile + 1];
    memset(file, 0, sizeoffile + 1);
    stream.read(file, sizeoffile);

    char _filename[512];
    memset(_filename, 0, 512);

    char *ptr = 0;
    if((ptr = strrchr(filename, '/')) > 0 || (ptr = strrchr(filename, '\\' ) ) > 0 )
    {
        memcpy(_filename, (ptr + 1), strlen((ptr + 1)));
    }
    else
    {
        memcpy(_filename, filename, strlen(filename));
    }

    Pasv();
    TransferType(ASCII);

    SendCommand("STOR", _filename);
    memset(ftpcommand_b, 0, 1500);
    if(recv(ftpcommand, ftpcommand_b, 1500, 0) == SOCKET_ERROR)
    {
        closesocket(ftpcommand);
        return -1;
    }
    Print("FTP_COMMAND[in]( %d ): %s", __LINE__, ftpcommand_b);

    int maxtime = 3000;
    setsockopt(ftpdata, SOL_SOCKET, SO_RCVTIMEO, (char*)maxtime, sizeof(int));

    while(filepos != sizeoffile)
    {
        int ssize = sizeoffile - filepos < 1500 ? sizeoffile - filepos : 1500;
        Print("FTP_DATA[in]( %d ): Data size: %d", __LINE__, ssize);

        if(send(ftpdata, &file[filepos], ssize, 0) == SOCKET_ERROR)
        {
            Print("FTP_DATA[in]( %d ): Failed to send data!", __LINE__);
            closesocket(ftpdata);
            return -1;
        }
        filepos += ssize;
    }

    closesocket(ftpdata);

    memset(ftpcommand_b, 0, 1500);
    if(recv(ftpcommand, ftpcommand_b, 1500, 0) == SOCKET_ERROR)
    {
        closesocket(ftpcommand);
        return -1;
    }
    Print("FTP_COMMAND[in]( %d ): %s ssize: %d", __LINE__, ftpcommand_b, filepos);
    stream.close();
    return 0;
}

Function for receiving data works like a charm, but sending data not so much. I'm aware of the fact that my code is not perfect, but I'm just trying to understand how the FTP works. I'm of course going to edit my code :)

Thank You in advance!

ProXicT
  • 1,903
  • 3
  • 22
  • 46
  • Did you send the command to put the FTP server into binary mode? – Galik Nov 24 '14 at 22:59
  • Thank You. Yes, I did, problem persists. – ProXicT Nov 24 '14 at 23:20
  • Please show the code that uses FTP in binary mode. The code you showed is still using ASCII mode, but not sending the file content in a format that is consistent with ASCII mode. – Remy Lebeau Nov 24 '14 at 23:48
  • Well, the binary read mode is usable either for ASCII files and binary files from what I know. This code is used to send ASCII and binary files as well, but for the binary files is just changed the `TransferType()` changed from ASCII `(a)` to binary `(i)`. – ProXicT Nov 24 '14 at 23:56

1 Answers1

0

I figured out the problem.

The problem was not with sending data, but in receiving them back. In the GetFile func I had TransferType set to binary and not ASCII as it was when sending the data, so the problem is solved :)

Anyway, thank You guys for help! :)

ProXicT
  • 1,903
  • 3
  • 22
  • 46