Need help with ftp client programming
Anyone familiar with socket programming?
please help~!!!
I am trying to implement very simple ftp client from this source file but when I compile it and run it I get the followings.
----------------------------------------------
linux2[19]% ./ftpclient ftp.gl.umbc.edu
Username>>mkim
Password>>1234
Broken pipe
----------------------------------------------
What does this Broken pipe mean?
How can I fix it?
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#define MYFTP_PORT 9999
char username[20];
char pwd[6];
char temp[10];
int client;
/***************************************************************/
void get_user_pwd(void) {
char buff[27],temp[10];
while(1) {
printf("\nUsername>>");
scanf("%s",username);
printf("\nPassword>>");
scanf("%s",pwd);
strcpy(buff,username);
strcat(buff," ");
strcat(buff,pwd);
write(client,buff,strlen(buff)+1);
read(client,temp,10);
if (strcmp(temp,"OK"))
printf("Unknown username/password");
else {
printf("Connected\n");
return;
}
}
}
/**************************************************************/
void get_file(char *fname) {
FILE *in;
char buffer[100];
int total=0;
in=fopen(fname,"w+");
strcpy(buffer,"OK");
write(client,buffer,strlen(buffer)+1);
read(client,buffer,100);
while(buffer[0]!='\0'){
fprintf(in,"%s",buffer);
total+=strlen(buffer);
strcpy(buffer,"OK");
write(client,buffer,strlen(buffer)+1);
read(client,buffer,100);
}
printf("\n%d bytes succesfully copied to %s ....\n",
total,fname);
fclose(in);
}
/**************************************************************/
void send_file(char *fname){
char line[100];
FILE *fd;
int total=0;
if((fd=fopen(fname,"r"))){
fgets(line,100,fd);
while(!feof(fd)){
write(client,line,strlen(line)+1);
total+=strlen(line);
read(client,line,100);
fgets(line,100,fd);
}
line[0]='\0';
write(client,line,1);
printf("\n%d bytes sent to server succesfully",total);
fclose(fd);
}
else{
printf("\nCan not open source file");
strcpy(line,"NOOK");
write(client,line,strlen(line)+1);
}
}
/**************************************************************/
void lchdir(char *path){
if(chdir(path)<0){
perror("\nInvalid directory path");
}
else
printf("\nNew local directory= %s",path);
}
/**************************************************************/
void lsfiles(void){
char buff[100],temp[5];
read(client,buff,strlen(buff)+1);
strcpy(temp,"OK");
if(!strcmp(buff,"NOOK")){
printf("\nCan not list files");
write(client,temp,strlen(temp)+1);
return;
}
while(buff[0]!='\0'){
printf("\n %s",buff);
write(client,temp,strlen(temp)+1);
read(client,buff,100);
}
}
/**************************************************************/
void get_command(void) {
char srcf_name[10],desf_name[10];
char buff[100];
char command[10];
/* FILE *fd; */
while(1) {
printf("\n>>");
scanf("%s",command);
if (!strcmp(command,"get")){
scanf(" %s %s",srcf_name,desf_name);
strcpy(buff,command);
strcat(buff," ");
strcat(buff,srcf_name);
write(client,buff,strlen(buff)+1);
read(client,temp,10);
if (strcmp(temp,"OK"))
printf("File not found");
else{
get_file(desf_name);
return;
}
}
else if(!strcmp(command,"put")){
scanf(" %s %s",srcf_name,desf_name);
strcpy(buff,command);
strcat(buff," ");
strcat(buff,desf_name);
write(client,buff,strlen(buff)+1);
read(client,buff,100);
send_file(srcf_name);
return;
}
if(!strcmp(command,"lcd")){
scanf("%s",buff);
lchdir(buff);
}
else if(!strcmp(command,"ls")){
strcpy(buff,command);
strcat(buff," x");
write(client,buff,strlen(buff)+1);
lsfiles();
}
else
printf("\nUnkonwn Command......");
}
}
/**************************************************************/
int main (int argc,char *argv[]){
struct sockaddr_in to;
struct hostent *host_info;
/* char buff[100]="", temp[100], eof_str[2]; */
if (argc!=2)
{
printf("Wrong Usage myftp ata.cs.hun.edu.tr");
exit(1);
}
if((client=socket(AF_INET,SOCK_STREAM,0))<0){
perror("creating stream socket");
exit(2);
}
if((host_info=gethostbyname(argv[1]))==NULL){
perror("unknown host ");
exit(3);
}
to.sin_family=host_info->h_addrtype;
memcpy((char*)&to.sin_addr,host_info->h_addr,
host_info->h_length);
to.sin_port=htons(MYFTP_PORT);
connect(client,(struct sockaddr *)&to,sizeof(to));
get_user_pwd();
get_command();
close(client);
exit(0);
return 0;
}