Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple FTP Client

Simple FTP Client

Scheduled Pinned Locked Moved C / C++ / MFC
htmlsysadminhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all. Im trying to make a simple FTP client that uploads files. Again im trying to make a client that uploads files and not downloads. I specified that so that we could get an understanding of what im trying to do here. Anyway i was refered to a site that deals with programming for FTP Servers. Heres the url: http://www.stanford.edu/class/ee284/pa.html he instructions were written for a UNIX programming but WinSock supports all the functions it calls for. The functions are the following: socket() bind() listen() getsockname() accept() Mind everyone finding a simple way to use these functions is NOT a simple task. But after spending countless hours using google/yahoo i came up with the following code: #include #include using namespace std; #define port 21 bool ftpmsg(const char *message); SOCKET dataSock; SOCKET socketbuffer; int main(){ const int SIZE=100; char msg[SIZE]; char server[SIZE]; char recvbuffer[900]; WORD wVersionRequested; SOCKADDR_IN addr; WSADATA wsa; struct sockaddr_in structSock; struct hostent *ftpserveraddy; wVersionRequested=MAKEWORD(2, 0); WSAStartup(wVersionRequested, &wsa); dataSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); printf("Please enter the Host name to connect to:"); cin.getline(server, SIZE); printf("Connecting to FTP Server: '%s'\n",server); if((ftpserveraddy = gethostbyname(server)) == NULL){ printf("DNS Lookup of '%s' failed!\n",msg); } structSock.sin_family = AF_INET; structSock.sin_port = htons(port); structSock.sin_addr.s_addr = *((unsigned long*)ftpserveraddy->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == SOCKET_ERROR){ cout << "Error " << endl; } recv(dataSock, recvbuffer, 512, 0); cout << recvbuffer << endl; while(1){ socketbuffer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); getsockname(socketbuffer, (LPSOCKADDR)&addr, NULL); bind(socketbuffer, reinterpret_cast(&addr), sizeof(SOCKADDR_IN)); listen(socketbuffer, 10); accept(socketbuffer, NULL,NULL); Sleep(1000); ftpmsg("USER user\r\n"); Sleep(1000); ftpmsg("PASS password\r\n"); Sleep(1000); ftpmsg("PORT\r\n"); Sleep(1000); ftpmsg("STOR file.txt\r\n"); Sleep(9000); ftpmsg("QUIT

    J P 2 Replies Last reply
    0
    • D dellthinker

      Hi all. Im trying to make a simple FTP client that uploads files. Again im trying to make a client that uploads files and not downloads. I specified that so that we could get an understanding of what im trying to do here. Anyway i was refered to a site that deals with programming for FTP Servers. Heres the url: http://www.stanford.edu/class/ee284/pa.html he instructions were written for a UNIX programming but WinSock supports all the functions it calls for. The functions are the following: socket() bind() listen() getsockname() accept() Mind everyone finding a simple way to use these functions is NOT a simple task. But after spending countless hours using google/yahoo i came up with the following code: #include #include using namespace std; #define port 21 bool ftpmsg(const char *message); SOCKET dataSock; SOCKET socketbuffer; int main(){ const int SIZE=100; char msg[SIZE]; char server[SIZE]; char recvbuffer[900]; WORD wVersionRequested; SOCKADDR_IN addr; WSADATA wsa; struct sockaddr_in structSock; struct hostent *ftpserveraddy; wVersionRequested=MAKEWORD(2, 0); WSAStartup(wVersionRequested, &wsa); dataSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); printf("Please enter the Host name to connect to:"); cin.getline(server, SIZE); printf("Connecting to FTP Server: '%s'\n",server); if((ftpserveraddy = gethostbyname(server)) == NULL){ printf("DNS Lookup of '%s' failed!\n",msg); } structSock.sin_family = AF_INET; structSock.sin_port = htons(port); structSock.sin_addr.s_addr = *((unsigned long*)ftpserveraddy->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == SOCKET_ERROR){ cout << "Error " << endl; } recv(dataSock, recvbuffer, 512, 0); cout << recvbuffer << endl; while(1){ socketbuffer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); getsockname(socketbuffer, (LPSOCKADDR)&addr, NULL); bind(socketbuffer, reinterpret_cast(&addr), sizeof(SOCKADDR_IN)); listen(socketbuffer, 10); accept(socketbuffer, NULL,NULL); Sleep(1000); ftpmsg("USER user\r\n"); Sleep(1000); ftpmsg("PASS password\r\n"); Sleep(1000); ftpmsg("PORT\r\n"); Sleep(1000); ftpmsg("STOR file.txt\r\n"); Sleep(9000); ftpmsg("QUIT

      J Offline
      J Offline
      JudyL_MD
      wrote on last edited by
      #2

      I'm not familiar with the FTP protocol so I can't address that portion of your code. However, I see some problems inside the while loop. 1) You don't check the status of any of your calls to socket, bind, listen or accept - you need to do so. I bet one of them is failing, and checking the error code returned by WSAGetLastError will help you determine what is wrong. 2) Assuming you do successfully create the new socket, you never destroy it at the bottom of the loop. 3) There is no way out of your while loop. Judy

      1 Reply Last reply
      0
      • D dellthinker

        Hi all. Im trying to make a simple FTP client that uploads files. Again im trying to make a client that uploads files and not downloads. I specified that so that we could get an understanding of what im trying to do here. Anyway i was refered to a site that deals with programming for FTP Servers. Heres the url: http://www.stanford.edu/class/ee284/pa.html he instructions were written for a UNIX programming but WinSock supports all the functions it calls for. The functions are the following: socket() bind() listen() getsockname() accept() Mind everyone finding a simple way to use these functions is NOT a simple task. But after spending countless hours using google/yahoo i came up with the following code: #include #include using namespace std; #define port 21 bool ftpmsg(const char *message); SOCKET dataSock; SOCKET socketbuffer; int main(){ const int SIZE=100; char msg[SIZE]; char server[SIZE]; char recvbuffer[900]; WORD wVersionRequested; SOCKADDR_IN addr; WSADATA wsa; struct sockaddr_in structSock; struct hostent *ftpserveraddy; wVersionRequested=MAKEWORD(2, 0); WSAStartup(wVersionRequested, &wsa); dataSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); printf("Please enter the Host name to connect to:"); cin.getline(server, SIZE); printf("Connecting to FTP Server: '%s'\n",server); if((ftpserveraddy = gethostbyname(server)) == NULL){ printf("DNS Lookup of '%s' failed!\n",msg); } structSock.sin_family = AF_INET; structSock.sin_port = htons(port); structSock.sin_addr.s_addr = *((unsigned long*)ftpserveraddy->h_addr_list[0]); memset(structSock.sin_zero, 0, 8); if(connect(dataSock, (sockaddr*)&structSock, sizeof(sockaddr)) == SOCKET_ERROR){ cout << "Error " << endl; } recv(dataSock, recvbuffer, 512, 0); cout << recvbuffer << endl; while(1){ socketbuffer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); getsockname(socketbuffer, (LPSOCKADDR)&addr, NULL); bind(socketbuffer, reinterpret_cast(&addr), sizeof(SOCKADDR_IN)); listen(socketbuffer, 10); accept(socketbuffer, NULL,NULL); Sleep(1000); ftpmsg("USER user\r\n"); Sleep(1000); ftpmsg("PASS password\r\n"); Sleep(1000); ftpmsg("PORT\r\n"); Sleep(1000); ftpmsg("STOR file.txt\r\n"); Sleep(9000); ftpmsg("QUIT

        P Offline
        P Offline
        Peter Weyzen
        wrote on last edited by
        #3

        Are you doing it this way because you don't want to use the support already in windows to do this? It's fairly easy...

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

        D 1 Reply Last reply
        0
        • P Peter Weyzen

          Are you doing it this way because you don't want to use the support already in windows to do this? It's fairly easy...

          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

          D Offline
          D Offline
          dellthinker
          wrote on last edited by
          #4

          Peter Weyzen wrote:

          Are you doing it this way because you don't want to use the support already in windows to do this? It's fairly easy...

          Its off topic questions like these that annoy the common person trying to accomplish something. But if you must know why I'm attempting to do this is because i want to. Now if you have a solution to the issue at hand please feel free to offer some advice based on the question. Does anyone else have a educational solution pertaining to the issue at hand? Thanx in advance!

          P B 2 Replies Last reply
          0
          • D dellthinker

            Peter Weyzen wrote:

            Are you doing it this way because you don't want to use the support already in windows to do this? It's fairly easy...

            Its off topic questions like these that annoy the common person trying to accomplish something. But if you must know why I'm attempting to do this is because i want to. Now if you have a solution to the issue at hand please feel free to offer some advice based on the question. Does anyone else have a educational solution pertaining to the issue at hand? Thanx in advance!

            P Offline
            P Offline
            Peter Weyzen
            wrote on last edited by
            #5

            You could have answered "no". But you had to get made and respond with 4 times as many words as I did. You also were not clear in your intent. I tried to help. Lame-o!

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

            D 1 Reply Last reply
            0
            • D dellthinker

              Peter Weyzen wrote:

              Are you doing it this way because you don't want to use the support already in windows to do this? It's fairly easy...

              Its off topic questions like these that annoy the common person trying to accomplish something. But if you must know why I'm attempting to do this is because i want to. Now if you have a solution to the issue at hand please feel free to offer some advice based on the question. Does anyone else have a educational solution pertaining to the issue at hand? Thanx in advance!

              B Offline
              B Offline
              Bram van Kampen
              wrote on last edited by
              #6

              I was going to suggest something, but now I won't in case I get my nose bitten off too. X|

              LateNightsInNewry

              1 Reply Last reply
              0
              • P Peter Weyzen

                You could have answered "no". But you had to get made and respond with 4 times as many words as I did. You also were not clear in your intent. I tried to help. Lame-o!

                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                D Offline
                D Offline
                dellthinker
                wrote on last edited by
                #7

                Peter Weyzen wrote:

                You could have answered "no". But you had to get made and respond with 4 times as many words as I did. You also were not clear in your intent. I tried to help. Lame-o!

                First of all my intention was already said hence the topic of the thread. But since i have to spell it out for you then i'll do that. Im Trying To Make A FTP Client That Uploads. Not Something That Downloads. Im Only Trying To Get It To Upload. I hope that was simple enough. Second of all, for the second poster. Its not my intention to flame anyone or be a jerk. My goal is to figure something out. If you were going to provide helpful suggestions then I would have gladly accepted it. But if your going to ask lame questions like "why dont i just use another FTP client" when i clearly said im trying to 'learn' how to make one. Well then how would you reply if the tables were turned?

                P 1 Reply Last reply
                0
                • D dellthinker

                  Peter Weyzen wrote:

                  You could have answered "no". But you had to get made and respond with 4 times as many words as I did. You also were not clear in your intent. I tried to help. Lame-o!

                  First of all my intention was already said hence the topic of the thread. But since i have to spell it out for you then i'll do that. Im Trying To Make A FTP Client That Uploads. Not Something That Downloads. Im Only Trying To Get It To Upload. I hope that was simple enough. Second of all, for the second poster. Its not my intention to flame anyone or be a jerk. My goal is to figure something out. If you were going to provide helpful suggestions then I would have gladly accepted it. But if your going to ask lame questions like "why dont i just use another FTP client" when i clearly said im trying to 'learn' how to make one. Well then how would you reply if the tables were turned?

                  P Offline
                  P Offline
                  Peter Weyzen
                  wrote on last edited by
                  #8

                  Dude, you weren't clear at all. From reading your post, your goal could have been either: * I need to FTP stuff to a server * I want to implement the FTP protocol myself. We're all here on CodeProject to get help and to share our experiences with others. If someone's not answering the question that you think you asked, then maybe you didn't ask your question clearly. And instead of flaming me for so-called off-topic... why not just phrase your question and your intent clearly.... and apologize for being such a wanker. Then I might help answer the question you thought you were asking.

                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups