![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
FTP and return code fetching
I'm writing an ftp client with gtk+2.0 and C. I looked at the RFC and when i log on i get the return code 220 which prompts for a user login. I send the login info and then attempt to fetch return code 331 from the server which indicates that it's time for a password. I opened up ethereal and found that i do get a prompt for 331 but my program doesn't pick up the 331. I'm fetching the return code the same way i did for the 220 only i don't get 331 so i can tell the user. Any help is appreciated. Here's a snipper of the relevant code:
I have defined the return codes previously in #define PASSWORD 331 etc. just for reference. You can ignore the str functions since i'm just making the send info, which i think is irrelevant to the problem. I end up getting junk in the second return code which is marked below. void
on_button8_clicked (GtkButton *button,
gpointer user_data)
{
int sockfd;
struct sockaddr_in their_addr;
struct hostent *h;
int retcode=0;
char retmsg[13]; //the return code i get, which i atoi for retcode
char gmsg[40]; // the thing i send
int len=0;
int numbytes=0;
int retcodelen=3;
int sendbytes=0;
GtkTextBuffer *buffer=NULL;
GtkTextIter iter;
GtkWidget *textboxen=lookup_widget(GTK_WIDGET(button),"textview1");
buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(textboxen));
GtkWidget *entry3=lookup_widget(GTK_WIDGET(button),"entry3");
char *fetchuser=gtk_entry_get_text(GTK_ENTRY(entry3));
GtkWidget *entry4=lookup_widget(GTK_WIDGET(button),"entry4");
char *fetchpass=gtk_entry_get_text(GTK_ENTRY(entry4));
GtkWidget *entry5=lookup_widget(GTK_WIDGET(button),"entry5");
char *fetchip=gtk_entry_get_text(GTK_ENTRY(entry5));
h=gethostbyname(fetchip);
sockfd=socket(AF_INET, SOCK_STREAM,0);
their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(PORT);
their_addr.sin_addr.s_addr=inet_addr(inet_ntoa(*((struct in_addr *)h->h_addr)));
memset(&(their_addr.sin_zero), '\0',8);
if (connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1){
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
gtk_text_buffer_insert (buffer, &iter, "Connection error. Please enter hostname or ip address.\n", -1);
}
else{
numbytes=recv(sockfd,retmsg,retcodelen,0);
retcode=atoi(retmsg);
printf("%s, %d",retmsg,retcode); //debug line, it works
if (retcode==LOGIN){
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
gtk_text_buffer_insert (buffer, &iter, "\nConnected, Logging In...", -1);
strcpy(gmsg,"USER ");
strcat(gmsg,fetchuser);
strcat(gmsg,"\r\n");
//printf("%s",gmsg); //debug line
len=strlen(gmsg);
sendbytes=send(sockfd,gmsg,len,0); // send user info, works
numbytes=recv(sockfd,retmsg,retcodelen-1,0);
retcode=atoi(retmsg);
printf("\nafter recv:%s,%d\n",retmsg,retcode); //debug line, junk ret.
if (retcode==PASSWORD){
strcpy(gmsg,"PASS ");
strcat(gmsg,fetchpass);
strcat(gmsg,"\r\n");
printf("%s",gmsg); //debug line
len=strlen(gmsg);
sendbytes=send(sockfd,gmsg,len,0);
numbytes=recv(sockfd,gmsg,retcodelen,0);
retcode=atoi(gmsg);
if(retcode==LOGGED_IN){
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 100);
gtk_text_buffer_insert (buffer, &iter, "Authentification Successful.\n", -1);
}
}
}
} //end else
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 877
Rep Power: 4
![]() |
YOu are only calling recv to get the 3 bytes of the return code, the server is also sending back CR and LF, which may be getting your reads and writes out of order.
I recommend writing a function that reads a complete line back from the server. Then you can call that and parse the resultant string for the reply. That way you can handle other returns that may not be exactly 3 bytes. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 4
![]() |
Thanks, that was the problem. I thought i could cut out the entire string of what the server sent me to the first 3 bytes and hopefully receive only the return codes.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|