概述
目录
- 1、功能
- 2、API
- 3、实现思路
- 4、代码展示
- 服务端
- 客户端
- 头文件
1、功能
客户端可以往服务端,上传、下载、查看、删除文件
和博文:ftp云盘功能一样
在博文ftp云盘的基础上服务端增加了共享内存
运行截图:
2、API
共享内存的相关API:进程间通信
memset//初始化
memcpy//拷贝
memcmp//比较,比较对象可以是结构体
3、实现思路
服务端:
1.socket等待连接,连接成功fork进程
2.父进程运行程序ser1
3.子进程连接2个共享内存,接收客户端socket发来的指令并通过共享内存1发送到ser1处理,等待执行结果读取共享内存2内容
4.ser1程序
创建2个共享内存,共享内存1接收指令,共享内存2发送指令执行结果
每隔1秒判断共享内存1的指令,指令不同开始执行内容,否则继续循环判断
每条指令都会返回数据,无论成功失败,因为ser有数据判断,没数据返回进程就会卡住
客户端:
和博文:ftp云盘一样
4、代码展示
服务端
ser代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "ser.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void serv_cl(int c_fd)
{
key_t key2;
key_t key;
int shid;
int shid2;
int a;
//char *shmatadd2;
//char *shmatadd;
struct Mcc buf;
struct Mcc *shmatadd;
struct Mcc *shmatadd2;
a = sizeof(struct Mcc)/1024+1;
key2 = ftok(".",'c');
key = ftok(".",'a');
shid2 = shmget(key2,1024*a,0);
shid = shmget(key,1024*a,0);
if(shid == -1){
perror("shid");
}
if(shid2 == -1){
perror("shid2");
}
//shmatadd = shmat(shid,0,0);
//shmatadd2 = shmat(shid2,0,0);
shmatadd2 = shmat(shid2,0,0);
shmatadd = shmat(shid,0,0);
printf("ser:shmatadd success..n");
while(1){
/*
初始化buf和共享内存内容,(防止内容错误)
读socket,拷贝内容到共享内存中
再次初始化buf内容(buf是因为防止put指令数据data出错,)
判断buf和共享内存内容是否一样,不一样代表指令执行完毕
读取执行结果发回客户端
收到quit指令,退出循环,进而退出进程
*/
//printf("ser/n");
memset(&buf,0,sizeof(buf));
memset(shmatadd2,0,sizeof(shmatadd2));
read(c_fd,&buf,sizeof(buf));
//printf("ser//n");
//strcpy(shmatadd,buf.cmd);
memcpy(shmatadd,&buf,sizeof(buf));
//printf("ser:cmd->%sn",buf.cmd);
//sleep(2);i
memset(&buf,0,sizeof(buf));
//memset(shmatadd2,0,sizeof(shmatadd2));
while(!memcmp(buf.data,shmatadd2->data,2));
//printf("while okn");
memcpy(&buf,shmatadd2,sizeof(buf));
//printf("ser buf.type%dn",buf.type);
write(c_fd,&buf,sizeof(buf));
if(!strncmp("quit",buf.cmd,4)){
printf("ser quitn");
break;
}
/*if(strcmp(buf.cmd,"quit") == 0){
printf("quitn");
break;
}*/
}
shmdt(shmatadd2);
shmdt(shmatadd);
}
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("usage: command listen_portn");
exit(-1);
}
int jieshou;
int fid;
int s_fd;
int c_fd;
struct sockaddr_in addr;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&addr.sin_addr);
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd < 0)
{
perror("socket");
exit(-1);
}
if(bind(s_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) < 0)
{
perror("bind");
exit(-1);
}
if(listen(s_fd,10) < 0)
{
perror("listen");
exit(-1);
}
jieshou = sizeof(struct sockaddr_in);
while(1)
{
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&jieshou);
if(c_fd < 0)
{
perror("accept");
exit(-1);
}
else
{
printf("client IP:%sn",inet_ntoa(c_addr.sin_addr));
}
fid = fork();
if(fid < 0)
{
perror("fork error!");
exit(-1);
}
else if(fid == 0)
{
sleep(1);
serv_cl(c_fd);
// printf("serv_cln");
}else{
//execl("./ser1","ser1",NULL);
system("./ser1");
// printf("ser systemn");
}
close(c_fd);
}
close(s_fd);
return 0;
}
ser1代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include "ser.h"
#include <sys/stat.h>
#include <fcntl.h>
int get_cmd_type(char *buf)
{
if(!strncmp("ls",buf,2)) return LS;
if(!strncmp("quit",buf,4)) return QUIT;
if(!strncmp("pwd",buf,3)) return PWD;
if(!strncmp("cd",buf,2)) return CD;
if(!strncmp("rm",buf,2)) return RM;
if(!strncmp("get",buf,3)) return GET;
if(!strncmp("put",buf,3)) return PUT;
return 100;
}
char *S_tok(char *a)
{
char *p;
p = strtok(a," ");
p = strtok(NULL," ");
return p;
}
void GetPut(struct Mcc *buf,int set)
{
char *p = NULL;
int fd;
char cmd[128];
strcpy(cmd,buf->cmd);
switch(set){
case GET:
p = S_tok(cmd);
if(access(p,F_OK) == -1)
{
buf->type = -1;
//printf("type:%cn",buf->type);
strcpy(buf->data,"NO This file!!!");
//write(c_fd,&buf,sizeof(buf));
}
else
{
fd = open(p,O_RDWR);
//buf.a = lseek(fd,0,SEEK_END);
//lseek(fd,0,SEEK_SET);
//printf("lek:%dn",buf.a);
//buf.getput = (char*)malloc(buf.a);
read(fd,buf->data,sizeof(buf->data));
//write(c_fd,&buf.type,sizeof(buf.type));
//write(c_fd,&buf.a,sizeof(buf.a));
//write(c_fd,buf.getput,buf.a);
close(fd);
//free(buf.getput);
}
break;
case PUT:
p = S_tok(cmd);
fd = open(p,O_RDWR|O_CREAT,0600);
if(fd == -1){
perror("open");
strcpy(buf->data,"open fail");
break;
//printf("open onn");
}
//buf.getput = (char*)malloc(buf.a);
//read(c_fd,buf.getput,buf.a);
//printf("a=%d,getput=%ldn",buf.a,strlen(buf.getput));
write(fd,buf->data,sizeof(buf->data));
close(fd);
//free(buf.getput);
memset(buf->data,0,sizeof(buf->data));
strcpy(buf->data,"put cuccess...");
break;
}
}
void Cd(struct Mcc *buf,int set)
{
char *p = NULL;
switch(set){
case RM:
system(buf->cmd);
//printf("rm ok!n");
strcpy(buf->data,"rm success...n");
break;
case CD:
{
p = S_tok(buf->cmd);
printf("cd=%sn",p);
if(chdir(p) == -1)
{
perror("cd");
strcpy(buf->data,"cd fail...n");
break;
}
strcpy(buf->data,"cd success...n");
//printf("cd ok!n");
break;
}
case PWD:
case LS:
{
FILE *f = popen(buf->cmd,"r");
fread(buf->data,1024,5,f);
pclose(f);
//printf("ls ok!n");
break;
}
}
}
int main()
{
key_t key;
key_t key2;
int shid;
int shid2;
int a;
//char *shmatadd2;
//char *shmatadd;
struct Mcc *shmatadd;
struct Mcc *shmatadd2;
int set = 100;
struct Mcc buf;
a = sizeof(struct Mcc)/1024+1;
key2 = ftok(".",'c');
key = ftok(".",'a');
shid2 = shmget(key2,1024*a,IPC_CREAT|0666);
shid = shmget(key,1024*a,IPC_CREAT|0666);
if(shid == -1){
perror("shid");
}
if(shid2 == -1){
perror("shid2");
}
//shmatadd = shmat(shid,0,0);
//shmatadd2 = shmat(shid2,0,0);
shmatadd2= shmat(shid2,0,0);
shmatadd = shmat(shid,0,0);
memcpy(shmatadd,&buf,sizeof(buf));
printf("ser1:shmatadd successn");
while(1){
/*
判断指令是否和上次一样,不一样才会执行指令
读取共享内存1
判断什么指令
根据判断执行相应指令
执行完往共享内存2放数据
进入下一次循环判断
接收到quit退出循环进而退出程序
*/
//memset(cmd,0,sizeof(cmd));
//if(strncmp(cmd,shmatadd,2)){
//strcpy(cmd,shmatadd);
//if(strncmp(cmd,shmatadd->cmd,2)){
if(memcmp(buf.cmd,shmatadd->cmd,2)){
memset(&buf,0,sizeof(buf));
memcpy(&buf,shmatadd,sizeof(buf));
//strcpy(cmd,buf.cmd);
//memset(&buf,0,sizeof(buf));
printf("ser1:buf.cmd=%sn",buf.cmd);
set = get_cmd_type(buf.cmd);
if(set < 0){
GetPut(&buf, set);
}else if((set < 20) && (set > 0)){
//strcpy(buf,Cd(buf,cmd,set));
Cd(&buf,set);
//buf = Cd(cmd,set);
//printf("buf:%sn",buf);
}else if(set == 0){
strcpy(buf.data,"quit...n");
}else{
strcpy(buf.data,"zhiling fail...n");
}
//printf("ser1 type+%cn",buf.type);
memcpy(shmatadd2,&buf,sizeof(buf));
}
if(set == QUIT){
printf("ser1 quitn");
break;
}
/*if((strcmp(cmd,"quit") == 0)){
printf("quitn");
break;
}*/
sleep(1);
}
shmdt(shmatadd);
if(shmctl(shid,IPC_RMID,0) == -1)
{
printf("shmctl non");
}
shmdt(shmatadd2);
if(shmctl(shid2,IPC_RMID,0) == -1)
{
printf("shmctl2 non");
}
return 0;
}
客户端
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "ser.h"
char *S_tok(char *a)
{
char *p;
p = strtok(a," ");
p = strtok(NULL," ");
return p;
}
int get_cmd_type(char *buf)
{
if(!strncmp("ls",buf,2)) return LS;
if(!strncmp("lls",buf,3)) return LLS;
if(!strncmp("quit",buf,4)) return QUIT;
if(!strncmp("pwd",buf,3)) return PWD;
if(!strncmp("lpwd",buf,4)) return LPWD;
if(!strncmp("cd",buf,2)) return CD;
if(!strncmp("lcd",buf,3)) return LCD;
if(!strncmp("rm",buf,2)) return RM;
if(!strncmp("lrm",buf,3)) return LRM;
if(!strncmp("get",buf,3)) return GET;
if(!strncmp("put",buf,3)) return PUT;
return 100;
}
void serv_client(int c_fd)
{
int fd;
struct Mcc buf;
char *p;
char pd[128];
char cmd[128];
printf(">>ls,cd,rm,pwd,lls,lcd,lrm,lpwd,quit,get,put<<n");
while(1)
{
printf("------------------------------------------------n");
memset(&buf,0,sizeof(buf));
printf(">");
//do{
// gets(buf.cmd);
//}while(!strncmp(cmd,buf.cmd,2));
gets(buf.cmd);
while(!strncmp(cmd,buf.cmd,2))
{
printf("command repeatn>");
gets(buf.cmd);
}
strcpy(cmd,buf.cmd);
buf.set = get_cmd_type(buf.cmd);
switch(buf.set)
{
case QUIT:
write(c_fd,&buf,sizeof(buf));
printf("cl quitn");
sleep(2);
return;
//exit(0);
case PWD:
case LS:
write(c_fd,&buf,sizeof(buf));
read(c_fd,&buf,sizeof(buf));
printf("%s",buf.data);
break;
case CD:
write(c_fd,&buf,sizeof(buf));
read(c_fd,&buf,sizeof(buf));
printf("%s",buf.data);
break;
case RM:
write(c_fd,&buf,sizeof(buf));
read(c_fd,&buf,sizeof(buf));
printf("%s",buf.data);
break;
case GET:
//p = S_tok(buf.cmd);
//strcpy(buf.cmd,p);
write(c_fd,&buf,sizeof(buf));
read(c_fd,&buf,sizeof(buf));
printf("buf.type = %dn",buf.type);
if(buf.type != -1)
{
//read(c_fd,&buf.a,sizeof(buf.a));
//buf.getput = (char*)malloc(buf.a);
//read(c_fd,buf.getput,buf.a);
p = S_tok(buf.cmd);
fd = open(p,O_RDWR|O_CREAT,0600);
if(fd == -1){
perror("open");
printf("open onn");
}
//printf("a=%d,getput=%ldn",buf.a,strlen(buf.getput));
write(fd,buf.data,sizeof(buf.data));
close(fd);
printf("get successn");
//free(buf.getput);
}
else
{
printf("%sn",buf.data);
}
break;
case PUT:
strcpy(cmd,buf.cmd);
p = S_tok(cmd);
//strcpy(buf.cmd,p);
if(access(p,F_OK) == -1)
{
printf("NO This file!!!n");
}
else
{
fd = open(p,O_RDWR);
//buf.a = lseek(fd,0,SEEK_END);
//lseek(fd,0,SEEK_SET);
//printf("lek:%dn",buf.a);
//buf.getput = (char*)malloc(buf.a);
read(fd,buf.data,sizeof(buf.data));
write(c_fd,&buf,sizeof(buf));
//write(c_fd,buf.getput,buf.a);
read(c_fd,&buf,sizeof(buf));
close(fd);
printf("%sn",buf.data);
//free(buf.getput);
}
break;
case LPWD:
case LLS:
case LRM:
p = buf.cmd;
p++;
system(p);
break;
case LCD:
p = S_tok(buf.cmd);
strcpy(buf.cmd,p);
printf("lcd=%sn",buf.cmd);
if(chdir(buf.cmd) == -1)
{
perror("why:");
}
printf("success......n");
break;
case 100:
write(c_fd,&buf,sizeof(buf));
read(c_fd,&buf,sizeof(buf));
printf("%s",buf.data);
break;
}
}
}
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("usage: command listen_portn");
exit(-1);
}
int fid;
int s_fd;
struct sockaddr_in addr;
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&addr.sin_addr);
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd < 0)
{
perror("socket");
exit(-1);
}
if(connect(s_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) == -1)
{
perror("connect");
exit(-1);
}
printf("client......n");
fid = fork();
if(fid < 0)
{
perror("fork error!");
exit(-1);
}
if(fid == 0)
{
serv_client(s_fd);
//exit(0);
}
wait(NULL);
close(s_fd);
return 0;
}
头文件
#define PWD 2
#define CD 3
#define RM 5
#define GET -1
#define PUT -2
#define LPWD 8
#define LCD 9
#define LRM 10
#define QUIT 0
struct Mcc
{
char data[1024*5];
char cmd[128];
int type;
int set;
};
最后
以上就是愤怒康乃馨为你收集整理的ftp云盘(socket网络,共享内存)1、功能2、API3、实现思路4、代码展示的全部内容,希望文章能够帮你解决ftp云盘(socket网络,共享内存)1、功能2、API3、实现思路4、代码展示所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复