Home Forums C Programming FTP uploading via winsock

Viewing 0 reply threads
  • Author
    Posts
    • #2205
      RosauraBWM
      Participant

      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
      #include
      #include
      #include
      #include
      #include
      #include

      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, "rn");

      send(_LSoc, username, strlen(username), 0);
      Sleep(1000);
      recv(_LSoc, servermessage, 1000, 0);

      strcat(password, passbuffer);
      strcat(password, "rn");

      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 Irn", 8, 0);
      Sleep(1000);
      recv(_CSoc, servermessage, 10000, 0);
      Sleep(1000);
      Sleep(1000);
      send(_CSoc, "PASVrn", 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.txtrn", strlen("STOR test.txtrn"), 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;
      }


Viewing 0 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.