title: Socket FTP in C: authentication gets 530 "Please login with user and pass" with anonymous
body: <p>I'm trying to connect to a server to download an POS (Point of sale) application. For now, I'm trying to make work on the PC to adapt to the POS. (The POS doen't have a FTP API, just socket)</p>
  <p>Trying to connect to <a href="ftp://ftp.inf.puc-rio.br/" rel="nofollow noreferrer">ftp://ftp.inf.puc-rio.br/</a> (139.82.16.194). I also tried ftp.dca.fee.unicamp.br with the same result.</p>
  <p>I'm using <em>sys/socket.h</em>. I'm getting this:</p>
  <blockquote>
  <p>220 Bem Vindo ao Servidor do DI / PUC-Rio.</p>
  <p><strong>USER anonymous</strong></p>
  <p>Response: 331 Please specify the password.</p>
  <p><strong>PASS 123</strong></p>
  <p>Response: 530 Please login with USER and PASS</p>
  </blockquote>
  <p>I tried another passowords like email, guest but no sucess.
  In <strong>Filezilla</strong> I use the same values (IP, user, password 123 and port 21) and I get this:</p>
  <blockquote>
  <p>USER anonymous</p>
  <p>Response: 331 Please specify the password.</p>
  <p>Comando:  PASS ***</p>
  <p>Response: 230 Login successful.</p>
  </blockquote>
  <p>Trying to understand why the server don't accept the PASS command.</p>
  <p>Here is my code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;time.h&gt;
  #include &lt;errno.h&gt;
  
  #define SERVER_PORT_ID 21
  #define CLIENT_PORT_ID 8015
  #define SERVER_HOST_ADDR &quot;139.82.16.194&quot;
  /* ftp.dca.fee.unicamp.br */
  //ftp://ftp.inf.puc-rio.br/
  #define MAXSIZE 256    
  
  main(int argc,char *argv[]){           
  
      int sockid, newsockid,i,getfile,ack,msg,msg_2,c,len;
      int no_writen,start_xfer, num_blks,num_last_blk;
      struct sockaddr_in my_addr, server_addr;
      FILE *fp;
      char in_buf[MAXSIZE], buffer[MAXSIZE];       
  
  
      if ((sockid = socket(AF_INET,SOCK_STREAM,0)) &lt; 0)
      {
          printf(&quot;client: socket error : %d\n&quot;, errno); exit(0);
          
      }
      
      printf(&quot;client: binding my local socket\n&quot;);
      bzero((char *) &amp;my_addr,sizeof(my_addr));
      my_addr.sin_family = AF_INET;
      my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      my_addr.sin_port = htons(CLIENT_PORT_ID);
      if (bind(sockid ,(struct sockaddr *) &amp;my_addr,sizeof(my_addr)) &lt; 0)
      {
          printf(&quot;client: bind error :%d\n&quot;, errno); exit(0);
  
      }
  
      setsockopt(sockid,SOL_SOCKET, SO_KEEPALIVE, 0, MAXSIZE);
  
      //Try to connect
      printf(&quot;client: starting connect\n&quot;);
      bzero((char *) &amp;server_addr,sizeof(server_addr));
      server_addr.sin_family = AF_INET;
      server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDR);
      server_addr.sin_port = htons(SERVER_PORT_ID);
      if (connect(sockid ,(struct sockaddr *) &amp;server_addr,
      sizeof(server_addr)) &lt; 0)
      {
          printf(&quot;client: connect error :%d\n&quot;, errno); exit(0);
  
      }
  
  //Print the Welcome message
      do{
          memset((void*) buffer,0,MAXSIZE);
          if(read(sockid,buffer,MAXSIZE) &lt; 0) 
          {
              printf(&quot;client: read error :%d\n&quot;, errno); exit(0);
          }       
          printf(&quot;%s \n&quot;, buffer);
      }while(strstr(buffer,&quot;220 &quot;) == NULL);
  
      printf(&quot;Mensagem Welcome recebida\n&quot;);
  
  //Try to connect
  
      stpcpy(buffer,&quot;USER anonymous\r\n&quot;);
      printf(&quot;%s&quot;, buffer);
      if(write(sockid,buffer,sizeof(buffer))&lt; 0){
          printf(&quot;client: write error :%d\n&quot;, errno); exit(0);
      }
      if(read(sockid,buffer,MAXSIZE) &lt; 0)   
      {
          printf(&quot;client: read error :%d\n&quot;, errno); exit(0);
      }
      printf(&quot;%s&quot;, buffer);
  
      memset((void*) buffer,0,MAXSIZE);
      stpcpy(buffer,&quot;PASS 123\r\n&quot;);  
      printf(&quot;%s&quot;, buffer);
      if(write(sockid,buffer,sizeof(buffer))&lt; 0){
          printf(&quot;client: write error :%d\n&quot;, errno); exit(0);
      }
      memset((void*) buffer,0,MAXSIZE);
      if(read(sockid,buffer,MAXSIZE) &lt; 0) 
      {
          printf(&quot;client: read error :%d\n&quot;, errno); exit(0);
      }
      printf(&quot;%s \n&quot;, buffer);
  
      exit(0);
  }
  </code></pre>
  <p>The code are not downloading anything. I'm trying to athenticate frist. Some includes and variables are not used because belongs to the FTP application's exemple that have the rest of code to download.</p>
  <p><a href="http://www.tcpipguide.com/free/t_FTPControlConnectionEstablishmentUserAuthenticatio-2.htm" rel="nofollow noreferrer">FTP Authentication</a></p>
  <p>Thanks everyone!</p>