Simple FTP Client
-
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
-
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
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
-
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
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)
-
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)
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!
-
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!
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)
-
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!
I was going to suggest something, but now I won't in case I get my nose bitten off too. X|
LateNightsInNewry
-
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)
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?
-
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?
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)