-
ftp connection error
Code:
#include <windows.h>
#include <stdio.h>
WSADATA ws;
int d;
char aa[1000];
char usr[100];
char paswd[100];
struct sockaddr_in a;
SOCKET s;
int ii;
struct hostent *host;
void abc(char *p)
{
FILE *fp = fopen("b.txt","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
d = WSAStartup(0x101,&ws);
sprintf(aa," WSASTARTUP = %d",d);
abc(aa);
s = socket(AF_INET,SOCK_STREAM,0);
sprintf(aa," SOCKET = %d",s);
abc(aa);
host=gethostbyname("smartfile.com");
a.sin_family = AF_INET;
a.sin_port = htons(21);
a.sin_addr.s_addr = *((unsigned long*)host->h_addr);
d = connect(s, (struct sockaddr *)&a, sizeof( a));
strcat(usr,"USER xyzab\r\n");
send(s,usr,sizeof(usr),0);
ii = 1;
ii = recv(s,aa,1000,0);
abc(aa);
strcat(paswd,"PASS abcdeg\r\n");
send(s,paswd,sizeof(paswd),0);
ii = 1;
ii = recv(s,aa,1000,0);
abc(aa);
}
I need to upload a file using ftp connection so i wrote this code output i get is
: WSASTARTUP = 0
SOCKET = 1940
220 208.88.252.31 FTP server ready
331 Password required for illusionist
500 Invalid command: try being more creative
rd required for illusionist
I can't figure out why password command give error
plz help.....
-
You are sending garbage to the server:
Code:
strcat(usr,"USER xyzab\r\n");
send(s,usr,sizeof(usr),0);
So what are you sending to the server here?
"USER xyzab\r\n\0<random garbage here>"
Instead of sending the entire buffer, you need to just send the string:
Code:
send(s,usr,strlen(usr),0);
Also, stop using strcat() where you should be using strcpy().
-
Thanx it worked now i have completed code to upload a text file to server code for that is:
Code:
#include <windows.h>
#include <stdio.h>
WSADATA ws;
int d;
char aa[1000];
char usr[100];
char paswd[100]={0};
char cmd[15]={0};
struct sockaddr_in a;
struct sockaddr_in a1;
SOCKET s;
SOCKET s1;
int ii;
struct hostent *host;
int calcp(char p1[])
{
int i,k;
int n;
char port1[10];
char port2[10];
int por;
i=k=n=por=0;
while(p1[i] != '(' )
i++;
n=0;
while( n < 4 )
{
if( p1[i] == ',')
n++;
i++;
}
k=0;
while(p1[i] != ',' )
{
port1[k++]=p1[i];
i++;
}
i++;
port1[k]='\0';
k=0;
while(p1[i] != ')' )
{
port2[k++]=p1[i];
i++;
}
port2[k++]='\0';
k=0;
por= atoi(port1)*256 + atoi(port2);
}
void abc(char *p)
{
FILE *fp = fopen("b.txt","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
int port=0;
d = WSAStartup(0x101,&ws);
sprintf(aa," WSASTARTUP = %d",d);
abc(aa);
s = socket(AF_INET,SOCK_STREAM,0);
sprintf(aa," SOCKET = %d",s);
abc(aa);
host=gethostbyname("smartfile.com");
a.sin_family = AF_INET;
a.sin_port = htons(21);
a.sin_addr.s_addr = *((unsigned long*)host->h_addr);
d = connect(s, (struct sockaddr *)&a, sizeof( a));
strcpy(usr,"USER xxxxxxx\r\n");
send(s,usr,strlen(usr),0);
sleep(1000);
ii = 1;
ii = recv(s,aa,1000,0);
abc(aa);
strcpy(paswd,"PASS xxxxxx\r\n");
send(s,paswd,strlen(paswd),0);
sleep(1000);
ii = 1;
ii = recv(s,aa,1000,0);
abc(aa);
send(s,"TYPE I\r\n",8,0);
sleep(1000);
ii = 1;
ii = recv(s,aa,1000,0);
abc(aa);
send(s,"PASV\r\n",6,0);
sleep(1000);
ii = 1;
ii = recv(s,aa,1000,0);
abc(aa);
port=calcp(aa);
s1 = socket(AF_INET,SOCK_STREAM,0);
sprintf(aa," SOCKET = %d",s1);
abc(aa);
a1.sin_family = AF_INET;
a1.sin_port = htons(port);
a1.sin_addr.s_addr = *((unsigned long*)host->h_addr);
d = connect(s1, (struct sockaddr *)&a1, sizeof( a1));
strcpy(cmd,"STOR /b.txt\r\n");
send(s1,cmd,strlen(cmd),0);
sleep(1000);
WSACleanup();
}
The code compiles well get executed but no file is uploaded any suggestions...
Plz