Hello ,

I have a source code that could potentially upload files onto an FTP server using winsock. But there seems to be a small glitch and the files are not getting uploaded. Could someone please guide me as to why this is happening. I would be forever indebted. thanks in advance.

#include <string>
#include <winsock.h>
#include <windows.h>
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <cstdlib>

using namespace std;

void stringtoint(const string &s, int &i){
   istringstream myStream(s);
   myStream>>i;
}

void sendLogIn(SOCKET _LSoc){
 char userbuffer[] = "username";                 
 char passbuffer[] = "password";                 
 char username[] = "USER ";
 char password[] = "PASS ";
 char servermessage[1000];
 
 strcat(username, userbuffer);
 strcat(username, "\r\n");
 
 send(_LSoc, username, strlen(username), 0);
 Sleep(1000);
    recv(_LSoc, servermessage, 1000, 0);

 strcat(password, passbuffer);
 strcat(password, "\r\n");
 
 send(_LSoc, password, strlen(password), 0);
 Sleep(1000);
    recv(_LSoc, servermessage, 1000, 0);
}

int sendConnInfo(SOCKET _CSoc){
 char servermessage[10000];
 char ftpmessage[50];
 string message;
 string portbuffer;
    string port1;
 string port2;
 size_t position;
 size_t position2;
 int port;
 int portbuf;
 int _portbuf;
    
 send(_CSoc, "TYPE I\r\n", 8, 0);
 Sleep(1000);
    recv(_CSoc, servermessage, 10000, 0);
 Sleep(1000);
 Sleep(1000);
    send(_CSoc, "PASV\r\n", 6, 0);
 Sleep(1000);
    recv(_CSoc, ftpmessage, 50, 0);
 
 message = ftpmessage;
 position = message.find("Mode");
 portbuffer = message.substr(position+21);
 
    position = portbuffer.find(",");
    position2 = portbuffer.find(">");
    
    port1 = portbuffer.substr(0, position);
    port2 = portbuffer.substr(position+1, position2-1);
    
    stringtoint(port1, portbuf);
    stringtoint(port2, _portbuf);
    
    port = portbuf*256;
    port = port + _portbuf;
    return port;
}

void sendFileRequest(SOCKET _FSoc){
     send(_FSoc, "STOR test.txt\r\n", strlen("STOR test.txt\r\n"), 0);
     Sleep(1000);
}

BOOL ftpSocket(int port){
     SOCKET sock;
     SOCKADDR_IN pasvserver;
     char servermessage[MAX_PATH];
     HANDLE HFile;
     DWORD read;
     char *buffer;
     char filename[] = "C:\\test.txt";
     int connectionerror2;
     int trycount2 = 2;

     
     sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
     if(sock == INVALID_SOCKET){
   WSACleanup();
   return 0;
  }
  
 pasvserver.sin_family = 2;
 pasvserver.sin_port = htons(port);                                    
 pasvserver.sin_addr.s_addr = inet_addr("66.220.9.50");              //Once again the drivehq ftp server
    
 connectionerror2 = connect(sock, (LPSOCKADDR)&pasvserver, sizeof(struct sockaddr));
     while(connectionerror2 == SOCKET_ERROR){
            connectionerror2 = connect(sock, (LPSOCKADDR)&pasvserver, sizeof(struct sockaddr));
            trycount2++;
            if(trycount2 = 10){
                         closesocket(sock);
                         WSACleanup();
                         return 0;
            }
    }
     

    HFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    buffer = (char *)malloc(4096);
    SetFilePointer(HFile, 0, NULL, FILE_BEGIN);
    
    while(ReadFile(HFile, buffer, 4096, &read, NULL) && read > 0){
         send(sock, buffer, read, 0);
    }
 
    return true;    
}



int sendFile(){
 FreeConsole();
 WSAData WData;
 SOCKET FSoc;
 SOCKADDR_IN server;
 int connectionerror;
 int trycount = 2;
 char servermessage[MAX_PATH];
 int port;
 
 WSAStartup(MAKEWORD(2,2), &WData);
 FSoc = socket(2, SOCK_STREAM, IPPROTO_TCP); 
        if(FSoc == INVALID_SOCKET){
   WSACleanup();
   return 0;
  } 
 
 server.sin_family = 2;
 server.sin_port = htons(21);
 server.sin_addr.s_addr = inet_addr("66.220.9.50"); //this is the drivehq ftp server address.
                                                       
 
 connectionerror = connect(FSoc, (LPSOCKADDR)&server, sizeof(struct sockaddr));
     while(connectionerror == SOCKET_ERROR){
            connectionerror = connect(FSoc, (LPSOCKADDR)&server, sizeof(struct sockaddr));
            trycount++;
            if(trycount = 10){
                         closesocket(FSoc);
                         WSACleanup();
                         return 0;
            }
    }
 
 recv(FSoc, servermessage, sizeof(servermessage),0);
 
 sendLogIn(FSoc);
 Sleep(1000);  //give the server and the client sometime to deal with the influx of new messages
               //so that data for the ip doesnt get mixed up. 
    port = sendConnInfo(FSoc);
    sendFileRequest(FSoc);
    ftpSocket(port);
    WSACleanup();
    return 0;
}
int main(){
   sendFile();
   return 1;
}

What error are you getting?

Well I am not getting any error as such. But am not able to upload the files to the ftp server as well. So have to rectify that. Please provide some help. Thanks in advance.

The password can not be verified, probably its just a fake username and password in the code your posted. You need to provide a valid user name and password -- but do NOT post them here.

Oh yes i did not actually post the real password and username. But inspite that , i can't get the file uploaded. Could you please help me provide an addition to the source code that specifies in which folder the file gets stored on the FTP server. Thanks.

why don't you just get one of the free open source FTP client programs?

commented: Good advice +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.