Send file to ftp server using sockets in C -


i trying upload file ftp server using sockets. connects server , can read directories doesn't upload file. "stor" command creates file in server empty content. why getting "no data connection" response server , file empty?

#include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h>  int main(int argc, char *argv[]) {     argv[0]="2";     argv[1]="ip";      int sockfd = 0, n = 0;     char recvbuff[1024];     struct sockaddr_in serv_addr;     char mesuser[100]="user ftpuser\n";     char mespass[100]="pass pass\n";     char mestor[100]="stor xx\n";     char mespasv[100]="pasv\n";     char meappe[100]="appe xx\n";      argc=2;      if(argc != 2)     {         printf("\n usage: %s <ip of server> \n",argv[0]);         return 1;     }      memset(recvbuff, '0',sizeof(recvbuff));     if((sockfd = socket(af_inet, sock_stream, 0)) < 0)     {         printf("\n error : not create socket \n");         return 1;     }      memset(&serv_addr, '0', sizeof(serv_addr));      serv_addr.sin_family = af_inet;     serv_addr.sin_port = htons(21);      if(inet_pton(af_inet, argv[1], &serv_addr.sin_addr)<=0)      {      printf("\n inet_pton error occured\n");      return 1;      }       //serv_addr.sin_addr.s_addr = inet_addr(argv[1]);      if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)     {         printf("\n error : connect failed \n");         return 1;     }      if( send(sockfd , mesuser , strlen(mesuser) , 0) < 0)     {         perror("send failed");         return 1;     }      if( send(sockfd , mespass , strlen(mespass) , 0) < 0)     {         perror("send failed");         return 1;     }      char* fr_name = "pic.jpg";     file *fr = fopen(fr_name, "r");     if(fr == null){         printf("file cannot opened");         return 0;     }     else{          /*if( send(sockfd , mespasv , strlen(mespasv) , 0) < 0)         {             perror("send failed");             return 1;         }*/          if( send(sockfd , mestor , strlen(mestor) , 0) < 0)         {             perror("send failed");             return 1;         }            char buffer[100];         fscanf(fr,"%s",buffer);         write(sockfd,buffer,100);           if( send(sockfd , meappe , strlen(meappe) , 0) < 0)         {             perror("send failed");             return 1;         }         //close(sockfd);       }      /*if( send(sockfd , mestor , strlen(mestor) , 0) < 0)     {         perror("send failed");         return 1;     }     */      while ( (n = read(sockfd, recvbuff, sizeof(recvbuff)-1)) > 0)     {         recvbuff[n] = 0;         if(fputs(recvbuff, stdout) == eof)         {             printf("\n error : fputs error\n");             return 0;         }     }      if(n < 0)     {         printf("\n read error \n");         return 0;     }       return 0; } 

output:

 220---------- welcome pure-ftpd [privsep] [tls] ---------- 220-you user number 6 of 50 allowed. 220-local time 14:41. server port: 21. 220 disconnected after 15 minutes of inactivity. 331 user username ok. password required 230 ok. current restricted directory / 425 no data connection 425 no data connection 

it looks have incomplete understanding of ftp protocol. ftp works on idea of having 2 socket connections between client , server. first "control" connection, , second "data" connection. on control connection, text commands sent , forth, doing in code. when data needs sent , forth, done using data connection.

there 2 ways ftp set data connection, described here:

  • active: client specifies port server, , server connects port on client's machine. bad because firewalls , nat designed avoid these sorts of connections.
  • passive: server gives client port connect to, , client connects port on server machine.

in brief, you'll need to:

  1. use pasv command.
  2. parse response server port number (probably using scanf).
  3. open data connection port on server.
  4. put file through data connection.

it's been while since last wrote sockets in c, , don't have time write example, should enough started. (here specification ftp. rfc's heavy reading, unambiguous , can answer lot of questions, if can learn read them.)


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -