项目要求:实现以下内容
远程控制:
1、查看服务器当前路径文件 ls
3、进入、退出服务器文件夹 cd
4、上传文件到服务器 put xxx
本地控制:
1、查看本地(客户端)文件 lls
2、进入客户端文件夹 lcd
3、获取服务器的文件 get xxx
服务器:解析指令(创建子进程、对接客户端)
1、创建套接字socket
2、绑定bind ip+端口号
3、listen监听
4、accept接受客户端请求,返回值小于0报错
4.1有客户端接入调用fork创建子进程,返回值小于0报错;==0对客户端进行读操作
4.2对读取出来的数据做判断(ls、get、cd、put)
4.2.1get:open打开文件,read文件,send文件到客户端
4.2.2 ls:popen文件,fread读取文件,send发送文件到客户端
5、可system运行一个副服务器,来执行指令与执行结果的存储
6、多个客户端接入实现
客户端:发指令
1、socket创建套接字
2、connect连接服务器
3、连接上以后获取用户的输入,对输入的信息进行处理
代码演示:一共分为3部分,一个服务器文件,一个客户端文件,一个config.h自定义头文件
1、config.h头文件
#define LS 0 #define GET 1 #define PWD 2 #define IFGO 3 #define LCD 4 #define LLS 5 #define CD 6 #define PUT 7 #define QUIT 8 #define DOFILE 9 struct Msg { int type; char data[1024]; char secondbuf[128]; };
2、服务器代码:sever.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #include "config.h" #include <fcntl.h> int strcmp_msg(