实现的功能
服务器端功能
支持文件的上传和下载、断开重连、查看服务器端文件、切换文件目录
客户端功能
主要是负责把文件上传到服务器,下载服务器文件到本地,查看本地文件等
具体指令
上传文件:put + 文件名
下载文件:get + 文件名
查看服务器文件:ls
查看客户端文件:lls
切换服务器文件目录:cd
查看服务器端工作目录路径 :pwd
断开连接quit
目的
为了进一步加深Linux 系统编程知识点的运用,详细的知识点在这
所用到的技术
文件的各种API、进程、线程、网络、socket
进入代码之前先了解一些API
1.判断文件是否存在access
int access(const char *pathname, int mode);
返回值:不存在返回-1
参数1:文件名字
参数2:这里用F_OK
R_OK 只判断是否有读权限
W_OK 只判断是否有写权限
X_OK 判断是否有执行权限
F_OK 只判断是否存在
2.改变当前目录chdir
int chdir(const char *path);
参数:指向目录的指针
3.字符串分割strtok
char *strtok(char *str, const char *delim);
/*用法例子*/
char a[128]="xiao wei !!!";
//char *a = "xiao wei !!!";
char *p;
printf("a = %s\n",a); // a = xiao wei !!!
p = strtok(a," ");
printf("p1 = %s\n",p); // p1 = xiao
printf("a = %s\n",a); // a = xiao
p = strtok(NULL," ");
printf("p2 = %s\n",p); // p2 = wei
printf("a = %s\n",a); // a = xiao
p = strtok(NULL," ");
printf("p3 = %s\n",p); // p3 = !!!
printf("a = %s\n",a); // a = xiao
p = strtok(NULL," ")
printf("p4 = %s\n",p); // p4 = (null)
printf("a = %s\n",a); // a = xiao
return 0;
服务器
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define LS 0
#define PWD 1
#define GET 2
#define IFGO 3
#define CD 4
#define PUT 5
#define LLS 6
#define LCD 7
#define LPWD 8
#define QUIT 9
#define DOFILE 10
struct Msg
{
int type;
char cmd[1024];
char secondBuf[128];
};
char* getdir(char *cmd)
{
char *p = NULL;
p = strtok(cmd, " ");
p = strtok(NULL, " ");
return p;
}
int getCmdType(char *cmd)
{
if(!strcmp("ls", cmd)) return LS;
if(!strcmp("lls", cmd)) return LLS;
if(!strcmp("pwd", cmd)) return PWD;
if(!strcmp("quit", cmd)) return QUIT;
if(strstr(cmd, "cd")) return CD;
if(strstr(cmd, "get")) return GET;
if(strstr(cmd, "put")) return PUT;
return -1;
}
void msg_handler(struct Msg msg, int fd)
{
char *file = NULL;
char dataBuf[1024] = {0};
int fdFile;
int ret = getCmdType(msg.cmd);
switch(ret){
case LS:
case PWD:
msg.type = 0;
FILE *r = popen(msg.cmd, "r");
fread(msg.cmd, sizeof(msg.cmd), 1, r);
write(fd, &msg, sizeof(msg));
break;
case CD:
msg.type = 1;
char *dir = getdir(msg.cmd);
printf("dir: %s\n", dir);
chdir(dir);
break;
case GET:
file = getdir(msg.cmd);
if(access(file, F_OK) == -1){
strcpy(msg.cmd, "No This file!");
write(fd, &msg, sizeof(msg));
}else{
msg.type = DOFILE;
fdFile = open(file, O_RDWR);
read(fdFile, dataBuf, sizeof(dataBuf));
close(fdFile);
strcpy(msg.cmd, dataBuf);
write(fd, &msg, sizeof(msg));
}
break;
case PUT:
fdFile = open(getdir(msg.cmd), O_RDWR|O_CREAT, 0666);
write(fdFile, msg.secondBuf, strlen(msg.secondBuf));
close(fdFile);
break;
case QUIT:
printf("client quit!\n");
exit(-1);
}
}
int main(int argc, char **argv)
{
int s_fd;
int c_fd;
int n_read;
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
struct Msg msg;
if(argc != 3){
printf("param is not good\n");
exit(-1);
}
memset(&s_addr, 0, sizeof(struct sockaddr_in));
// 1.socket
s_fd = socket(AF_INET, SOCK_STREAM, 0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
// 2.bind
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(struct sockaddr_in));
// 3.listen
listen(s_fd, 10);
// 4.accept
int clen = sizeof(struct sockaddr_in);
while(1){
c_fd = accept(s_fd, (struct sockaddr *)&c_addr, &clen);
if(c_fd == -1){
perror("accept");
}
printf("get connect :%s\n", inet_ntoa(c_addr.sin_addr));
if(fork() == 0){
while(1){
memset(msg.cmd, 0, sizeof(msg.cmd));
// 5.read
n_read = read(c_fd, &msg, sizeof(msg));
if(n_read == 0){
printf("client out\n");
break;
}else if(n_read > 0){
msg_handler(msg, c_fd);
}
}
}
}
// 7.close
close(s_fd);
close(c_fd);
return 0;
}
客户端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define LS 0
#define PWD 1
#define GET 2
#define IFGO 3
#define CD 4
#define PUT 5
#define LLS 6
#define LCD 7
#define LPWD 8
#define QUIT 9
#define DOFILE 10
struct Msg
{
int type;
char cmd[1024];
char seconBuf[1280];
};
char* getdir(char *cmd)
{
char *p = NULL;
p = strtok(cmd, " ");
p = strtok(NULL, " ");
return p;
}
int getCmdType(char *cmd)
{
if(!strcmp("ls", cmd)) return LS;
if(!strcmp("lls", cmd)) return LLS;
if(!strcmp("pwd", cmd)) return PWD;
if(!strcmp("quit", cmd)) return QUIT;
if(strstr(cmd, "cd")) return CD;
if(strstr(cmd, "get")) return GET;
if(strstr(cmd, "put")) return PUT;
return -1;
}
int cmd_handler(struct Msg msg, int c_fd)
{
int ret;
char buf[32] = {0};
int filefd;
char *dir = NULL;
ret = getCmdType(msg.cmd);
switch(ret){
case LS:
case CD:
case PWD:
msg.type = 0;
printf("msg: %s\n", msg.cmd);
write(c_fd, &msg, sizeof(msg));
break;
case GET:
msg.type = 2;
write(c_fd, &msg, sizeof(msg));
break;
case PUT:
strcpy(buf, msg.cmd);
dir = getdir(buf);
if(access(dir, F_OK) == -1){
printf("%s not exsit\n", dir);
}else{
filefd = open(dir, O_RDWR);
read(filefd, msg.seconBuf, sizeof(msg.seconBuf));
close(filefd);
write(c_fd, &msg, sizeof(msg));
}
break;
case LLS:
system("ls");
break;
case LCD:
dir = getdir(msg.cmd);
chdir(dir);
break;
case QUIT:
strcpy(msg.cmd, "quit");
write(c_fd, &msg, sizeof(msg));
close(c_fd);
exit(-1);
}
return ret;
}
void serverMsgHandler(struct Msg msg, int c_fd)
{
int n_read;
struct Msg msgGet;
char *dir = NULL;
int newFile;
n_read = read(c_fd, &msgGet, sizeof(msgGet));
if(n_read == 0){
printf("server is out, quit\n");
exit(-1);
}else if(msgGet.type == DOFILE){
dir = getdir(msg.cmd);
newFile = open(dir, O_RDWR|O_CREAT, 0600);
write(newFile, msgGet.cmd, strlen(msgGet.cmd));
putchar('>');
fflush(stdout);
}else{
printf("==========================\n");
printf("\n%s\n", msgGet.cmd);
printf("===========================\n");
putchar('>');
fflush(stdout);
}
}
int main(int argc, char **argv)
{
int c_fd;
int ret;
struct sockaddr_in c_addr;
struct Msg msg;
if(argc != 3){
printf("param is not good\n");
exit(-1);
}
// 1.socket
c_fd = socket(AF_INET, SOCK_STREAM, 0);
if(c_fd == -1){
perror("socket");
exit(-1);
}
// 2.connect
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1], &c_addr.sin_addr);
int con = connect(c_fd, (struct sockaddr *)&c_addr, sizeof(struct sockaddr));
if(con == -1){
perror("connect");
exit(-1);
}
printf("connect---------------------\n");
int mark = 0;
// 3.write
while(1){
memset(msg.cmd, 0, sizeof(msg.cmd));
if(mark == 0){
printf(">");
}
gets(msg.cmd);
if(strlen(msg.cmd) == 0){
if(mark == 1){
continue;
}
}
mark = 1;
ret = cmd_handler(msg, c_fd);
if(ret > IFGO){
putchar('>');
fflush(stdout);
continue;
}
if(ret == -1){
printf("cmd not \n");
printf(">");
fflush(stdout);
continue;
}
// 4.read
serverMsgHandler(msg, c_fd);
}
//close
close(c_fd);
return 0;
}
运行结果
ftp云盘项目