Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Problem read/write to a socket opened for FTP

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 12-03-2008   #1 (permalink)
Just Joined!
 
Join Date: Dec 2008
Posts: 9
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!
ariel_ro is offline  


Reply With Quote
Old 12-03-2008   #2 (permalink)
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 1,192
When you say:
Quote:
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.
wje_lf is offline   Reply With Quote
Old 12-03-2008   #3 (permalink)
Just Joined!
 
Join Date: Dec 2008
Posts: 9
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.
ariel_ro is offline   Reply With Quote
Old 12-04-2008   #4 (permalink)
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.
wje_lf is offline   Reply With Quote
Old 12-04-2008   #5 (permalink)
Just Joined!
 
Join Date: Dec 2008
Posts: 9
Actually, I didn't append the "\n" to the end. Thank you very much! It works fine know!
ariel_ro is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 09:31 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2