功能
- 客户端与服务端通过socket建立连接后,客户端发送命令,服务端完成相应服务
- 命令:
命令 | 说明 |
---|
ls | 查看服务端文件 | pwd | 查看服务端路径 | cd | 服务端切换目录 | myls | 查看本地文件 | mypwd | 查看本地路径 | mycd | 本地切换目录 | get | 从服务端下载文件到本地 | put | 上传文件到服务端 | quit | 客户端退出 |
代码
头文件:
#ifndef __MYFTP_H__
#define __MYFTP_H__
#define LS 0
#define CD 1
#define PWD 2
#define GET 3
#define PUT 4
#define MYLS 5
#define MYPWD 6
#define MYCD 7
#define QUIT 8
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
struct msg{
char cmd[32];
char buf[1024*8];
};
int clientCmdHandle(struct msg data,int fd);
int serverCmdHandle(struct msg data,int fd);
#endif
用到的函数:
#include "myftp.h"
struct msg data = {"\0","\0"};
char* getdir(char *p){
p = strtok(p," ");
p = strtok(NULL," ");
return p;
}
int getCmd(char *cmd)
{
if(!strcmp(cmd,"ls")) return LS;
else if(!strcmp(cmd,"pwd")) return PWD;
else if(strstr(cmd,"get") != NULL) return GET;
else if(strstr(cmd,"put") != NULL) return PUT;
else if(!strcmp(cmd,"myls")) return MYLS;
else if(!strcmp(cmd,"mypwd")) return MYPWD;
else if(strstr(cmd,"mycd") != NULL) return MYCD;
else if(strstr(cmd,"cd") != NULL) return CD;
else if(!strcmp(cmd,"quit")) return QUIT;
else{
printf("%s not found!\n",cmd);
return -1;
}
}
int fileSize(char* filename)
{
struct stat statbuf;
stat(filename,&statbuf);
int size=statbuf.st_size;
return size;
}
int getFile(char* name,struct msg data,int fd)
{
read(fd,&data,sizeof(data));
int size = strlen(data.buf);
if(size == 0){
printf("File size 0 \n");
return -1;
}
int fileFd = open(name,O_RDWR|O_CREAT|O_EXCL,0600);
if(fileFd == -1){
perror("error");
return -1;
}
write(fileFd,data.buf,size);
close(fileFd);
printf("get %s %d byte...\n",name,size);
return 0;
}
int putFile(char* name,struct msg data,int fd)
{
int fileFd = open(name,O_RDWR);
if(fileFd == -1){
printf("No file\n");
return -1;
}
int size = fileSize(name);
if(size > sizeof(data.buf)){
printf("file is too big\n");
return -1;
}
read(fileFd,data.buf,size);
close(fileFd);
write(fd,&data,sizeof(data));
printf("File sent...\n");
}
int clientCmdHandle(struct msg data ,int fd)
{
char *dir = NULL;
int size = 0;
FILE *fp = NULL;
int cmd = getCmd(data.cmd);
fflush(stdin);
switch(cmd){
case LS:
case CD:
case PWD:
write(fd,&data,sizeof(data));
read(fd,&data,sizeof(data));
printf("%s\n",data.buf);
break;
case MYLS:
fp = popen("ls","r");
fread(data.buf,sizeof(data.buf),1,fp);
printf("%s\n",data.buf);
pclose(fp);
break;
case MYPWD:
fp = popen("pwd","r");
fread(data.buf,sizeof(data.buf),1,fp);
printf("%s\n",data.buf);
pclose(fp);
break;
case MYCD:
dir = getdir(data.cmd);
chdir(dir);
break;
case GET:
write(fd,&data,sizeof(data));
dir = getdir(data.cmd);
getFile(dir,data,fd);
break;
case PUT:
write(fd,&data,sizeof(data));
dir = getdir(data.cmd);
putFile(dir,data,fd);
break;
case QUIT:
write(fd,&data,sizeof(data));
return QUIT;
default: return -1;
}
}
int serverCmdHandle(struct msg data ,int fd)
{
char *dir = NULL;
FILE *fp = NULL;
int cmd = getCmd(data.cmd);
printf("get cmd:%s\n\n",data.cmd);
fflush(stdin);
switch(cmd){
case LS:
fp = popen("ls","r");
fread(data.buf,sizeof(data.buf),1,fp);
pclose(fp);
write(fd,&data,sizeof(data));
break;
case CD:
dir = getdir(data.cmd);
chdir(dir);
case PWD:
fp = popen("pwd","r");
fread(data.buf,sizeof(data.buf),1,fp);
pclose(fp);
write(fd,&data,sizeof(data));
break;
case MYLS:
case MYPWD:
case MYCD:
break;
case GET:
dir = getdir(data.cmd);
putFile(dir,data,fd);
break;
case PUT:
dir = getdir(data.cmd);
getFile(dir,data,fd);
break;
case QUIT:
write(fd,&data,sizeof(data));
return QUIT;
default: return -1;
}
}
服务端代码:
#include "myftp.h"
int main(int argc,char**argv)
{
int n_read;
int ret;
struct msg data = {"\0","\0"};
if(argc != 3){
printf("Invalid parameter\n");
exit(1);
}
int c_fd;
int s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socket error");
exit(1);
}
struct sockaddr_in s_addr;
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
inet_aton("argv[1]",&s_addr.sin_addr);
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(s_addr));
listen(s_fd,10);
struct sockaddr_in c_addr;
int c_size = sizeof(c_addr);
while(1){
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_size);
if(c_fd == -1){
perror("accept error");
exit(1);
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0){
while(1){
memset(&data,0,sizeof(data));
n_read = read(c_fd,&data,sizeof(data));
if(n_read == 0){
printf("\n\n%s--------------------------------------v\n>",inet_ntoa(c_addr.sin_addr));
printf("client %s out\n",inet_ntoa(c_addr.sin_addr));
printf("\n-----------------------------------------------^\n");
close(c_fd);
break;
}else if(n_read > 0){
printf("\n\n%s--------------------------------------v\n>",inet_ntoa(c_addr.sin_addr));
ret = serverCmdHandle(data,c_fd);
if(ret == QUIT){
printf("client %s out\n",inet_ntoa(c_addr.sin_addr));
printf("\n------------------------------------------------^\n");
close(c_fd);
break;
}
printf("\n-----------------------------------------------^\n");
}
}
}
}
return 0;
}
客户端代码:
#include "myftp.h"
int main(int argc,char**argv)
{
int ret;
struct msg data = {"\0","\0"};
if(argc != 3){
printf("Invalid parameter\n");
exit(1);
}
int c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1){
perror("socket error");
exit(1);
}
struct sockaddr_in c_addr;
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&c_addr.sin_addr);
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(c_addr)) == -1){
perror("connect error");
exit(1);
}
while(1){
printf("\n>");
memset(&data,0,sizeof(data));
gets(data.cmd);
ret = clientCmdHandle(data,c_fd);
printf("\n--------------------------------------\n");
if(ret == QUIT){
close(c_fd);
printf("Disconnected...\n");
return 0;
}
}
return 0;
}
测试
客户端:
$ ./client.out 192.168.8.104 6666
>cd ..
/home/yuechengjie/Studay/Linux
--------------------------------------
>ls
1.c
file
myFTP
process
shellJio
socket
thread
--------------------------------------
>myls
233.txt
client.c
client.out
myftp.c
myftp.h
server.c
server.out
--------------------------------------
>put 233.txt
File sent...
--------------------------------------
>get 1.c
get 1.c 209 byte...
--------------------------------------
>quit
--------------------------------------
Disconnected...
服务端:
$ ./server.out 127.0.0.1 6666
get connect:192.168.8.104
192.168.8.104--------------------------------------v
>get cmd:cd ..
-----------------------------------------------^
192.168.8.104--------------------------------------v
>get cmd:ls
-----------------------------------------------^
192.168.8.104--------------------------------------v
>get cmd:put 233.txt
get 233.txt 108 byte...
-----------------------------------------------^
192.168.8.104--------------------------------------v
>get cmd:get 1.c
File sent...
-----------------------------------------------^
192.168.8.104--------------------------------------v
>get cmd:quit
client 192.168.8.104 out
------------------------------------------------^
|