Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:42:21+00:00 2026-06-08T21:42:21+00:00

I’m trying to implement a simple FTP client using winsock. I’m having problems trying

  • 0

I’m trying to implement a simple FTP client using winsock. I’m having problems trying to download a file. Here’s the code I’m using at the moment:

bool FTPHandler::downloadFile(const char * remoteFilePath, const char * filePath) {
    if (!isConnected()) {
         setErrorMsg("Not connected, imposible to upload file...");
         return false;
     }   

     if (usePasiveMode) {
         this->pasivePort = makeConectionPasive();
         if (this->pasivePort == -1) {
             //error msg will be setted by makeConectionPasive()
             return false;
         }
     } else {
         setErrorMsg("Unable to upload file not in pasive mode :S");
         return false;
     }

     char * fileName = new char[500];
     getFileName(remoteFilePath,fileName);     

     // Default name and path := current directory and same name as remote.
     if (filePath == NULL) {
          filePath = fileName;
     }

     if (!setDirectory(remoteFilePath)) {
         return false;
     }



    char msg[OTHER_BUF_SIZE];
    char serverMsg[SERVER_BUF_SIZE];
    sprintf(msg,"%s%s\n",RETR_MSG,fileName);
    send(sock, msg, strlen(msg), 0);

     SOCKET passSocket;
     SOCKADDR_IN passServer;

     passSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (passSocket == INVALID_SOCKET) {
         WSACleanup();  
         sprintf(errorMsg,"Error trying to create socket (WSA error code: %d)",WSAGetLastError());
         return false;
     }

     passServer.sin_family = PF_INET;
     passServer.sin_port =   htons(this->pasivePort);
     passServer.sin_addr = *((struct in_addr *)gethostbyname(this->host)->h_addr);
     memset(server.sin_zero,0,8);

     int errorCode = connect(passSocket, (LPSOCKADDR) &passServer, sizeof(struct sockaddr));
     int tries = 0;
     while (errorCode == SOCKET_ERROR) {
           tries++;
           if (tries >= MAX_TRIES) {
               closesocket(passSocket);
               sprintf(errorMsg,"Error trying to create socket");
               WSACleanup();
               return false;
           }
     }

     char * buffer = (char *) malloc(CHUNK_SIZE);
     ofstream f(filePath);

     Sleep(WAIT_TIME);
     while (int readBytes = ***recv(passSocket, buffer, CHUNK_SIZE, 0)***>0) {
           buffer[readBytes] = '\0';
           f.write(buffer,readBytes);
     }
     f.close();

     Sleep(WAIT_TIME);
     recv(sock, serverMsg, OTHER_BUF_SIZE, 0);
     if (!startWith(serverMsg, FILE_STATUS_OKEY_CODE)) {
         sprintf(errorMsg,"Bad response: %s",serverMsg);
         return false;
     }

     return true;
}

That last recv() returns 1 byte several times, and then the method ends and the file that should be around 1Kb is just 23 bytes.

Why isn’t recv reading the hole file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:42:22+00:00Added an answer on June 8, 2026 at 9:42 pm

    There are all kinds of logic holes and incorrect/missing error handling in this code. You really need to clean up this code in general.

    You are passing the wrong sizeof() value to connect(), and not handling an error correctly if connect() fails (your retry loop is useless). You need to use sizeof(sockaddr_in) or sizeof(passServer) instead of sizeof(sockaddr). You are also not initializing passServer correctly.

    You are not checking recv() for errors. And in the off-chance that recv() actually read CHUCK_SIZE number of bytes then you have a buffer overflow that will corrupt memory when you write the null byte into the buffer (which you do not need to do) because you are writing it past the boundaries of the buffer.

    If connect() fails, or recv() fails with any error other than a server-side initiated disconnect, you are not telling the server to abort the transfer.

    Once you tell the server to go into Passive mode, you need to connect to the IP/Port (not just the Port) that the server tells you, before you then send your RETR command.

    Don’t forget to send the server a TYPE command so it knows what format to send the file bytes in, such as TYPE A for ASCII text and TYPE I for binary data. If you try to transfer a file in the wrong format, you can corrupt the data. FTP’s default TYPE is ASCII, not Binary.

    And lastly, since you clearly do not seem to know how to program sockets effectively, I suggest you use the FTP portions of the WinInet library instead of WinSock directly, such as the FtpGetFile() function. Let WinInet handle the details of transferring FTP files for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.