Find the answer to your Linux question:
Results 1 to 5 of 5
Hello everyone! I have to build a FTP Client in C/C++ under Linux and right now I'm stuck. I can't seem to read responses from the FTP server after I've ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    10

    Problem read/write to a socket opened for FTP



    Hello everyone!
    I have to build a FTP Client in C/C++ under Linux and right now I'm stuck. I can't seem to read responses from the FTP server after I've successfully connected to the server. After I send "USER myuser<CRLF>" the read function halts the execution.

    Code:
    int ConnectToServer::connectFTP()
    {
    	/* crearea socketului */
    	if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
    	{
    		cout<<"Eroare la socket()"<<endl;
    		return errno;
    	}
    
    	server.sin_family = AF_INET; /* familia socket-ului */
    	server.sin_addr.s_addr = inet_addr(_host.c_str()); /* adresa IP a serverului */
    	server.sin_port = htons (21); /* portul de conectare */
    		
            /* conectarea la server */
            if ( connect (sd, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1)
    	{
    	  cout<<"Eroare la connect()"<<endl;
    	  return errno;
    	}
    
            char _reply[255], _reply2[255], _reply3[255];
    	bzero(_reply, 255);
    	bzero(_reply2, 255);
    	bzero(_reply3, 255);
    	// citirea raspunsului dat de server  dupa crearea conexiunii
    	if (read (sd, &_reply, sizeof(_reply)) < 0)
    	{
    	  cout<<"Eroare la read() de la server."<<endl;
    	  return errno;
    	}
    	string response = _reply;
    	cout<<"Am primit de la server: "<<atoi(response.substr(0, 3).c_str())<<endl;
    	cout<<response<<endl;	
    		
    	while(true)
    	{
    	   // crearea comenzii pentru trimiterea user-ului
               string _comm("USER " + _username + "<CRLF>");
    		cout<<_comm<<endl;
    	    if (write (sd, &_comm, sizeof(_comm)) <= 0)
    	    {
    	        cout<<"Eroare la write() spre server. "<<endl;
    	        return errno;
    	    }
    	    fflush(stdout);
    	
                // citirea raspunsului dupa trimiterea username-ului 
    	    if (read (sd, &_reply2, sizeof(_reply2)) < 0)
    	    {
    		 cout<<"Eroare la read() de la server."<<endl;
    		 return errno;
    	    }
                cout<<"Lungimea celui de-al doilea raspuns "<<strlen(_reply2)<<endl;
    	    cout<<_reply2<<endl;
    		
                // trimiterea comenzii coresp. parolei
    	    string _comm2("PASS " + _password + "<CRLF>");
    	    cout<<_comm2<<endl;
    	    if (write (sd, &_comm2, sizeof(_comm2)) <= 0)
    	    {
    	          cout<<"Eroare la write() spre server. "<<endl;
    	          return errno;
    	    }
    	    fflush(stdout);
    		
    	    // citirea raspunsului dupa trimiterea parolei 
    	    if (read (sd, &_reply3, sizeof(_reply3)) < 0)
               {
    		  cout<<"Eroare la read() de la server."<<endl;
    		  return errno;
    	    }
    	    cout<<"Lungimea celui de-al treilea raspuns "<<strlen(_reply3)<<endl;
    	    cout<<_reply3<<endl;
    	    if(strlen(_reply3) > 3)
    		break;
    	}
    	return 0;	
    }
    Here is the function that does the actual login. I wrapped the read/write sequence into a infinite loop(saw this in some other snippets). Any help would be greatly appreciated!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    When you say:
    the read function halts the execution
    I assume mean that you execute this code:
    Code:
    		 cout<<"Eroare la read() de la server."<<endl;
    		 return errno;
    Is that correct?

    If it is, then you need to do something like this instead:
    Code:
                     int error_temp;
                     error_temp=errno;
    		 cout<<"Eroare la read() de la server."<<endl;
    		 return error_temp;
    to guard against the possibility that errno is modified before you can return it.

    Once you've done that, could you please tell us what value was found in errno?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    10
    Hi! Thanks for the reply wje_lf!
    Actually, it doesn't execute that portion. I connect to the desired FTP host, I get the response number 220 and than the program seems to wait for the response from the server(should be 331 User name ok, need password<CRLF>), which doesn't come. I guess it has something to do with the blocking behaviour of the read() function. I'm a beginner when it comes to C++ in Linux so my knowledge is limited.

    P.S.: I know that the first assumption might be that FTP server isn't working. Well, I manage to login successfully using regular ftp command and other clients.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Maybe it's not the read(). Maybe it's the preceding write().

    You send the user name to the server, correct? Do you follow that with a line feed (that is, a new line, that is, "\n")? Otherwise, the server will sit there waiting for the rest of the user name.

    If you already send down the "\n", could I see the code that includes the "\n"?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Dec 2008
    Posts
    10
    Actually, I didn't append the "\n" to the end. Thank you very much! It works fine know!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •