#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netdb.h>
#define CMD_LS 0x01
#define CMD_GET_FILE 0x02
#define MAGIC "55AA"
#define LS_BUF MAGIC"LS"
#define GET_BUF MAGIC"GET"
#define BUFSIZE 100
struct cmd_t
{
int cmdid;
char buf[100];
};
struct cmd_t parse(char *buf,int size)
{
struct cmd_t cmd;
bzero(cmd.buf,sizeof(cmd.buf));
if(strcmp(buf,LS_BUF) == 0)
{
cmd.cmdid = CMD_LS;
}
if(strncmp(buf,GET_BUF,strlen(GET_BUF)) == 0)
{
cmd.cmdid = CMD_GET_FILE;
strncpy(cmd.buf,buf + strlen(GET_BUF),(strlen(buf) - strlen(GET_BUF)));
}
return cmd;
}
int FtpServer()
{
int port = 2001;
int sd;
struct sockaddr_in addr;
int maxclient = 10;
int ret = 0, len;
socklen_t socklen;
pid_t pid;
char sbuf[BUFSIZE] = {0};
char rbuf[BUFSIZE] = {0};
sd = socket(AF_INET,SOCK_STREAM,0);
if(sd < 0)
{
printf("fail to create a socket\n");
return -1;
}
bzero((char*)&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if(bind(sd,(struct sockaddr*)&addr,sizeof(addr)) < 0)
{
printf("fail to bind\n");
return -1;
}
if(listen(sd,maxclient) < 0)
{
printf("fail to listen\n");
return -1;
}
printf("waiting ...\n");
while(1)
{
struct sockaddr_in caddr;
socklen = sizeof(caddr);
int csd = accept(sd,(struct sockaddr*)&caddr,&socklen);
if(csd < 0)
{
printf("fail to accpet\n");
return -1;
}
/*建立子进程*/
pid = fork();
if(pid > 0)
{
/*在父进程中,关闭connected socket*/
close(csd);
/*监听while开头*/
continue;
}
else if(pid == 0)
{
if(fork() > 0)
{
close(csd);
exit(0);
}
printf("the client address is: %s, port is: %d\n", inet_ntoa(caddr.sin_addr),caddr.sin_port);
read(csd,rbuf,BUFSIZE);
struct cmd_t cmd = parse(rbuf,strlen(rbuf));
if(cmd.cmdid == CMD_LS)
{
char buffer[100] = {0};
ls(".",buffer,100);
write(csd,buffer,strlen(buffer));
}
else if(cmd.cmdid == CMD_GET_FILE)
{
int fd = open(cmd.buf,O_RDONLY);
char buffer_file[BUFSIZE] = {0};
int ret;
while(1)
{
bzero(buffer_file,BUFSIZE);
ret = 0;
if((ret = read(fd,buffer_file,BUFSIZE -1)) <= 0)
break;
buffer_file[ret]='\0';
printf("file buf:%s\n",buffer_file);
write(csd,buffer_file,strlen(buffer_file));
}
close(fd);
}
else
{
printf("Server Fail");
}
close(csd);
}
}
close(sd);
}
int ls(const char *dir,char* buf,int size)
{
DIR* dir_p;
struct dirent* ptr;
int count = 0;
if((dir_p = opendir(dir)) == NULL)
{
printf("fail to open the dir %s",dir);
return -1;
}
while(( ptr = readdir(dir_p)) != NULL)
{
if(!strcmp(ptr->d_name,".") || !strcmp(ptr->d_name,".."))
{
continue;
}
//sbuf[]=ptr->d_name;
strncpy((buf + strlen(buf)),ptr->d_name,strlen(ptr->d_name));
buf[strlen(buf)] = ',';
buf[strlen(buf) + 1] = '\0';
}
return closedir(dir_p);
}
int main()
{
FtpServer();
}
#if 0
if(write(csd,sbuf,strlen(sbuf)) < 0)
{
printf("fail to write to client\n");
ret = -1;
break;
}
sleep(1);
if((len = read(csd,rbuf,BUFSIZE)) < 0)
{
printf("fail to read from client\n");
ret = -1;
break;
}
else if(len == 0)
{
break;
}
rbuf[len] = '\0';
printf("From client: %s",rbuf);
}
close(csd);
}
close(sd);
return ret;
}
#endif
#if 0
int ls(char *dir)
{
DIR* dir_p;
struct dirent* ptr;
int count = 0;
if((dir_p = opendir(dir)) == NULL)
{
printf("fail to open the dir %s",dir);
return -1;
}
while(( ptr = readdir(dir_p)) != NULL)
{
if(!strcmp(ptr->d_name,".") || !strcmp(ptr->d_name,".."))
{
continue;
}
sbuf[]=ptr->d_name;
count++;
if(count % 5 == 0)
printf("\n");
}
while(1)
{
int csd = accept(sd,(struct sockaddr*)&caddr,&socklen);
if(write(csd,sbuf,strlen(sbuf)) < 0)
{
printf("fail to write to client\n");
ret = -1;
break;
}
sleep(1);
if((len = read(csd,rbuf,BUFSIZE)) < 0)
{
printf("fail to read from client\n");
ret = -1;
break;
}
else if(len == 0)
{
break;
}
rbuf[len] = '\0';
printf("From client: %s",rbuf);
}
printf("\n");
return closedir(dir_p);
}
int get_file()
{
}
int multi_usr()
{
}
int main(int argc,char **argv)
{
TCPServer();
if(argv=="ls") ls("/home/newtry1");
else if(argv=="get_file") get_file();
else if (argv=="multi_usr") multi_usr();
else printf("Please input right command:ls,get_file or multi_usr");
}
#endif