473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C ftp programing - errors

Hi,
I have ftp client doen by using C program, when I complie its comin with a

warning: data definition has no type or storage class

& when i run it it comes as

Segmentation fault

Pls find below the coding

/**
* client.c
* @compile gcc client.c -o client
*/

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<netdb.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <string.h>
#define MAXBUFF 1024

str_echo(char * buff);
int iscdcmd(char * buff);//cd\u547d\u4ee4
int isgetcmd(char * k); //get\u547d\u4ee4

char * getPath(char *buff);//\u5f97\u5230\u8def\u5f84
char* getFile(char* cmd);//\u5f97\u5230\u4e0a\u4f20\u6587\u4ef6\u7684\u540d\u 79f0
int checkcmd(char* cmd);//\u76d1\u6d4b\u662f\u5426\u662f\u5408\u6cd5\u547d\u 4ee4
str_echo(char * buff)
{

char * s=buff;
if((strcmp(buff,"ls\n")==0)||(strcmp(buff,"pwd\n") ==0))
{
FILE * res=popen(buff,"r");
while(fgets(buff,MAXBUFF-1,res)!=NULL)
{
printf("%s",buff);
}
pclose(res);
}
else
printf("An invalid FTP command\n");
}

int iscdcmd(char * k)
{
char * s=k;
while(*s==' ')
{
s=s+1;
}
if(*s!='c')return 0;
s=s+1;
if(*s!='d')return 0;
s=s+1;
if(*s!=' ')return 0;
else return 1;
}
int isgetcmd(char * k)
{
char * s=k;
while(*s==' ')
{
s=s+1;
}
if(*s!='g')return 0;
s=s+1;
if(*s!='e')return 0;
s=s+1;
if(*s!='t')return 0;
s=s+1;
if(*s!=' ')return 0;
else return 1;
}
/**int isputcmd(char * k)
{
char * s=k;
while(*s==' ')
{
s=s+1;
}
if(*s!='p')return 0;
s=s+1;
if(*s!='u')return 0;
s=s+1;
if(*s!='t')return 0;
s=s+1;
if(*s!=' ')return 0;
else return 1;
} */

int checkcmd(char* cmd)
{
if(*cmd=='c'&&*(cmd+1)=='d')return 1;
else
if(*cmd=='l'&&*(cmd+1)=='s')return 1;
else
if(*cmd=='p'&&*(cmd+1)=='w'&&*(cmd+2)=='d')return 1;
else
if(*cmd=='g'&&*(cmd+1)=='e'&&*(cmd+2)=='t')return 1;
/** else
if(*cmd=='p'&&*(cmd+1)=='u'&&*(cmd+2)=='t')return 1;
*/
else
if(*cmd=='q'&&*(cmd+1)=='u'&&*(cmd+2)=='i'&&*(cmd+ 3)=='t')return 1;
else
return 0;

}
char* getPath(char* cmd)
{
char* Path;
while(*cmd==' ')
{
cmd=cmd+1;
}
if(*cmd=='c'&&*(cmd+1)=='d')
{
cmd=cmd+2;
while(*cmd==' ')
{
cmd=cmd+1;
}
Path=cmd;
while(*cmd!='\n')
{
cmd=cmd+1;
}
*cmd='\0';
}
return Path;
}
char* getFile(char* cmd)
{
char* Path;
while(*cmd==' ')
{
cmd=cmd+1;
}
cmd=cmd+3;
while(*cmd==' ')
{
cmd=cmd+1;
}
Path=cmd;
while(*cmd!='\n')
{
cmd=cmd+1;
}
*cmd='\0';

return Path;
}

main(argc,argv)
int argc;
char* argv[];
{
char buff[MAXBUFF];
char buff1[MAXBUFF];
char num[MAXBUFF];
char file[30];
struct sockaddr_in remote;
int sk,result,n,source,target,k;
struct hostent *hp, *gethostbyname();
sk=socket(AF_INET,SOCK_STREAM,0);
remote.sin_family=AF_INET;
hp=(struct hostent *)gethostbyname(argv[1]);
bcopy(hp->h_addr,(char*)&remote.sin_addr,hp->h_length);
remote.sin_port=atoi(argv[2]);
connect(sk,(struct sockaddr*)&remote,sizeof(remote));
if(read(sk,buff,MAXBUFF)>0)
{
printf("You are Client %s!\n",buff);
strcpy(num,buff);
printf("The server host name:%s\n",inet_ntoa(remote.sin_addr));

}
while(1)
{
printf("ftp>:");
fgets(buff,MAXBUFF,stdin);
if(*buff=='!')
{
char *k=buff;
k++;
if(iscdcmd(k))
{
result=chdir((char*)getPath(k));
if(result==0)
{
FILE* fp=popen("pwd","r");
if((fgets(buff,MAXBUFF,fp))!=NULL)
{
printf("The client directory is : %s\n",buff);

}
pclose(fp);
}
else
{
strcpy(buff,(char *)strerror(errno));
printf(buff);
printf("\n");
}
}
else
{
str_echo(k);
}
}
else
{
if(checkcmd(buff))
{
if(write(sk,buff,MAXBUFF)<0)
{
printf("Clent: Fail to pass the command !\n");
}
/**
if(isputcmd(buff)) //\u4e0a\u4f20\u6587\u4ef6
{

strcpy(file,getFile(buff));

if((source=open(file,O_RDONLY))<0)
{
printf("filename: no such file.\n");
write(sk,"error\n",MAXBUFF);
}
else
{
write(sk,"ok\n",MAXBUFF);
printf("uploading..\n");
lseek(source,0,SEEK_SET);
memset(buff, 0x0 ,MAXBUFF);
while((k=read(source,buff,MAXBUFF))>0)
{
write(sk,buff,k);
}
printf("finished\n");
}
close(source);


}
else */

if(isgetcmd(buff)) //\u4e0b\u8f7d\u6587\u4ef6
{

read(sk,buff1,MAXBUFF);
if(strcmp(buff1,"error\n")!=0)
{
printf("downloading..\n");
strcpy(file,getFile(buff));
printf(file);
if((target=open(file,O_WRONLY|O_CREAT|O_TRUNC,0644 ))<0)
{
printf("error\n");
}
while(1)
{
if((k=read(sk,buff,MAXBUFF))>0)
{
write(target,buff,k);
if(k!=MAXBUFF)break;
}
}
close(target);
printf("finished\n");

}
else printf("filename: no such file.\n");

}
else
{
if(strcmp(buff,"quit\n")==0)//\u9000\u51fa
{
break;
}
else
while(1) //\u8bfb\u53d6\u670d\u52a1\u5668\u7684\u8fd4\u56de\u 5b57\u7b26
{
if((n=read(sk,buff,MAXBUFF))>0)
{
if(strcmp(buff,"end")==0)break;
printf("%s",buff);
}
}
}
}
else
printf("An invalid FTP command\n");
}
}
close(sk);
}

Pls advice me.
Thanks
mili
Mar 11 '08 #1
1 2281
ashitpro
542 Expert 512MB
To get rid of the warning put "void" return type for following function(in definition as well as in declaration):
str_echo(char * buff);

Regarding your segmentation fault:
I am wondering how do you run your "client" binary
If you are running it as
./client
then I guess this code is not written by you. Because it takes command line arguments: first argument is server name and second argument is port probably
So you should run this binary something like this:
./client localhost 123
Change your command line argument as per your choice
Mar 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: chuck clark | last post by:
Hi, I am sorry if this is not the most appropriate group to ask this in (if not, please point me in the right direction). I am following examples from this page,...
2
by: Vlado | last post by:
I have db2 7.2 on Win 2000, programing MS VB6.0 I update 1 field on 600 records - successfully, but if i update more then 600 records then i have err. mesage SQL0952N Processing was cancelled...
2
by: cyshao | last post by:
How to reset Router by programing? For some resean, we need usually reset our Router. Now, we have to Reset Router manually(shot down and reopen). Are there any method to control and reset...
13
by: Xah Lee | last post by:
there's this one i can't figure out. I'm trying to kick out image.google.com's top frame when it goes to my page. If you go to http://images.google.com/images?q=milk+tits click on the page...
12
by: Xah Lee | last post by:
Frameset Infinity! http://xahlee.org/js/frame2/frameset.html HTML Frame tutorial + Infinity! http://xahlee.org/js/frame/0.html Xah xah@xahlee.org ∑ http://xahlee.org/
26
by: ashu | last post by:
lets look at the code given below here i m trying to do mudular programming /* this is my main prog.*/ /*mmod.c*/ #include<stdio.h> #include "mod1.h" int main(void) {
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
2
by: sengpg345 | last post by:
hi everybody. i have a project in colage for messanger but i haven't idea of socket programing in c#.net . so plz give me the guidence i socket programing. if u have row matirial or tutorial then...
2
by: maamirj | last post by:
Hi, I am newly start prgraming from very beigning using .net c# 2005 can u help me the concepts of programing, oops from begining and how to start programing wih right techniques. thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.