Article 5S9B3 Simple FTP client works great in C, but not in C++ : 500 Unknown Command

Simple FTP client works great in C, but not in C++ : 500 Unknown Command

by
rootaccess
from LinuxQuestions.org on (#5S9B3)
I wrote an FTP client in C that just uploads a file or buffer to a remote server and it works beautifully. But now porting it over to c++ has been a nightmare. Im a bit new to C++ but I have been messing around with it for a bit now. I know C very well so there are just some things that are different in C++ that bother me, but oh well.

It seems like there is a network problem because any command I send after I issue the PASV command and connect to it with a new socket, returns with 500 unknown command. Even bye, quit or even another PASV command returns with the same error. in C, works like a charm. From my windows machine, it just doesn't work. I'm 100% sure it is a network problem but I don't understand why. I am able to login and get the pasv port connected to. But I cannot send anymore data at all. I don't want to post the whole code because it isn't necessary, so i am not adding all the variables, and the main, brackets etc. So here are the important entries:
Code: #include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <string>
#include <ws2tcpip.h>
#pragma comment (lib, "Ws2_32.lib")

int sockfd, psockfd;
int bufsize;
int n, ret;
char msg1[1024];
struct sockaddr_in servaddr, cliaddr;
WSADATA wsaData;

if (WSAStartup(MAKEWORD(2, 0),&wsaData) != 0)
{
printf("WSAStartup() failed\n");
}

sockfd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr = inet_addr(host);
servaddr.sin_port=htons(port);
connect(sockfd,(SA*)&servaddr,sizeof(servaddr));


/* Login with USER */
send(sockfd, usr, user.length(), 0);
n=recv(sockfd,msg1,MAXSZ,0);
msg1[n] = '\0';
printf("%s\n", msg1);

/* Here are blocks similar to above with the password, then another
one for PASV, then i connect on pasv_port which I computed from
sscanf from the return after issuing PASV (huge pain):
*/

psockfd=socket(AF_INET,SOCK_STREAM,0);
cliaddr.sin_family=AF_INET;
cliaddr.sin_addr.s_addr = inet_addr(host);
cliaddr.sin_port=htons(pasv_port);
connect(psockfd,(SA*)&cliaddr,sizeof(cliaddr));


/* Send the STOR command */
send(sockfd,"STOR blah.txt\r\n", strlen("STOR blah.txt\r\n"), 0);
n=recv(sockfd,msg1,MAXSZ,0);
msg1[n] = '\0';
printf("%s\n", msg1);Then I get the 500 unknown command right after I connect with the new socket psockfd on the new pasv port. Like I said, all this works beautifuly in linux using my C code but maybe with windows, there is something I am missing. I am compiling using `-lws2_32`.

I also wanted to mention I was able to upload a file using telnet entirely from my windows box so I know there isn't any actual networking problem. But I believe that there is some library or syntax wrong with the way I am setting up the sockets but I don't know what it is in C++. Any help would be appreciated. Thanks!latest?d=yIl2AUoC8zA latest?i=8b-xTZuGFDQ:ZASQo-aOhb4:F7zBnMy latest?i=8b-xTZuGFDQ:ZASQo-aOhb4:V_sGLiP latest?d=qj6IDK7rITs latest?i=8b-xTZuGFDQ:ZASQo-aOhb4:gIN9vFw
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments