Home › Forums › C Programming › FTP uploading via winsock
- This topic has 0 replies, 1 voice, and was last updated 15 years, 6 months ago by RosauraBWM.
Viewing 0 reply threads
- AuthorPosts
- June 12, 2009 at 10:51 am #2205RosauraBWMParticipant
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185<br /><br /><br /><br />#include <string><br />#include <winsock.h><br />#include <windows.h><br />#include <sstream><br />#include <iostream><br />#include <stdio.h><br />#include <cstdlib><br /><br />using namespace std;<br /><br />void stringtoint(const string &s, int &i){<br />istringstream myStream(s);<br />myStream>>i;<br />}<br /><br />void sendLogIn(SOCKET _LSoc){<br />char userbuffer[] = "username";<br />char passbuffer[] = "password";<br />char username[] = "USER ";<br />char password[] = "PASS ";<br />char servermessage[1000];<br /><br />strcat(username, userbuffer);<br />strcat(username, "rn");<br /><br />send(_LSoc, username, strlen(username), 0);<br />Sleep(1000);<br />recv(_LSoc, servermessage, 1000, 0);<br /><br />strcat(password, passbuffer);<br />strcat(password, "rn");<br /><br />send(_LSoc, password, strlen(password), 0);<br />Sleep(1000);<br />recv(_LSoc, servermessage, 1000, 0);<br />}<br /><br />int sendConnInfo(SOCKET _CSoc){<br />char servermessage[10000];<br />char ftpmessage[50];<br />string message;<br />string portbuffer;<br />string port1;<br />string port2;<br />size_t position;<br />size_t position2;<br />int port;<br />int portbuf;<br />int _portbuf;<br /><br />send(_CSoc, "TYPE Irn", 8, 0);<br />Sleep(1000);<br />recv(_CSoc, servermessage, 10000, 0);<br />Sleep(1000);<br />Sleep(1000);<br />send(_CSoc, "PASVrn", 6, 0);<br />Sleep(1000);<br />recv(_CSoc, ftpmessage, 50, 0);<br /><br />message = ftpmessage;<br />position = message.find("Mode");<br />portbuffer = message.substr(position+21);<br /><br />position = portbuffer.find(",");<br />position2 = portbuffer.find(">");<br /><br />port1 = portbuffer.substr(0, position);<br />port2 = portbuffer.substr(position+1, position2-1);<br /><br />stringtoint(port1, portbuf);<br />stringtoint(port2, _portbuf);<br /><br />port = portbuf*256;<br />port = port + _portbuf;<br />return port;<br />}<br /><br />void sendFileRequest(SOCKET _FSoc){<br />send(_FSoc, "STOR test.txtrn", strlen("STOR test.txtrn"), 0);<br />Sleep(1000);<br />}<br /><br />BOOL ftpSocket(int port){<br />SOCKET sock;<br />SOCKADDR_IN pasvserver;<br />char servermessage[MAX_PATH];<br />HANDLE HFile;<br />DWORD read;<br />char *buffer;<br />char filename[] = "C:\test.txt";<br />int connectionerror2;<br />int trycount2 = 2;<br /><br /><br />sock = socket(2, SOCK_STREAM, IPPROTO_TCP);<br />if(sock == INVALID_SOCKET){<br />WSACleanup();<br />return 0;<br />}<br /><br />pasvserver.sin_family = 2;<br />pasvserver.sin_port = htons(port);<br />pasvserver.sin_addr.s_addr = inet_addr("66.220.9.50"); //Once again the drivehq ftp server<br /><br />connectionerror2 = connect(sock, (LPSOCKADDR)&pasvserver, sizeof(struct sockaddr));<br />while(connectionerror2 == SOCKET_ERROR){<br />connectionerror2 = connect(sock, (LPSOCKADDR)&pasvserver, sizeof(struct sockaddr));<br />trycount2++;<br />if(trycount2 = 10){<br />closesocket(sock);<br />WSACleanup();<br />return 0;<br />}<br />}<br /><br /><br />HFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);<br /><br />buffer = (char *)malloc(4096);<br />SetFilePointer(HFile, 0, NULL, FILE_BEGIN);<br /><br />while(ReadFile(HFile, buffer, 4096, &read, NULL) && read > 0){<br />send(sock, buffer, read, 0);<br />}<br /><br />return true;<br />}<br /><br /><br /><br />int sendFile(){<br />FreeConsole();<br />WSAData WData;<br />SOCKET FSoc;<br />SOCKADDR_IN server;<br />int connectionerror;<br />int trycount = 2;<br />char servermessage[MAX_PATH];<br />int port;<br /><br />WSAStartup(MAKEWORD(2,2), &WData);<br />FSoc = socket(2, SOCK_STREAM, IPPROTO_TCP);<br />if(FSoc == INVALID_SOCKET){<br />WSACleanup();<br />return 0;<br />}<br /><br />server.sin_family = 2;<br />server.sin_port = htons(21);<br />server.sin_addr.s_addr = inet_addr("66.220.9.50"); //this is the drivehq ftp server address.<br /><br /><br />connectionerror = connect(FSoc, (LPSOCKADDR)&server, sizeof(struct sockaddr));<br />while(connectionerror == SOCKET_ERROR){<br />connectionerror = connect(FSoc, (LPSOCKADDR)&server, sizeof(struct sockaddr));<br />trycount++;<br />if(trycount = 10){<br />closesocket(FSoc);<br />WSACleanup();<br />return 0;<br />}<br />}<br /><br />recv(FSoc, servermessage, sizeof(servermessage),0);<br /><br />sendLogIn(FSoc);<br />Sleep(1000); //give the server and the client sometime to deal with the influx of new messages<br />//so that data for the ip doesnt get mixed up.<br />port = sendConnInfo(FSoc);<br />sendFileRequest(FSoc);<br />ftpSocket(port);<br />WSACleanup();<br />return 0;<br />}<br />int main(){<br />sendFile();<br />return 1;<br />}<br /><br /><br /></cstdlib></iostream></sstream></string>
- AuthorPosts
Viewing 0 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.