config.h头文件
#define LS 0
#define GET 1
#define PWD 2
#define IFGO 3
#define CD 4
#define LCD 5
#define LLS 6
#define PUT 7
#define QUIT 8
#define DOFILE 9
struct Msg{
int type;
char cmd[1024];
char sendBuf[1024];
};
server服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
char *getDesDir(char *cmd)
{
char *p;
p = strtok(cmd," ");
p = strtok(NULL," ");
return p;
}
int get_cmd_type(char *cmd)
{
if(strcmp("ls",cmd) == 0){ return LS;}
if(strcmp("pwd",cmd) == 0){ return PWD;}
if(strcmp("quit",cmd) == 0){ return QUIT;}
if(strstr(cmd,"cd") != NULL){ return CD;}
if(strstr(cmd,"get") != NULL){ return GET;}
if(strstr(cmd,"put") != NULL){ return PUT;}
return 100;
}
void msg_handler(struct Msg msg, int fd)
{
char *file = NULL;
int fdfile;
char cmdBuf[1024] = {0};
printf("cmd:%s\n",msg.cmd);
int ret = get_cmd_type(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));
pclose(r);
break;
case CD:
msg.type = 1;
char *dir = getDesDir(msg.cmd);
printf("dir:%s\n",dir);
chdir(dir);
break;
case GET:
file = getDesDir(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, cmdBuf, sizeof(cmdBuf));
close(fdfile);
strcpy(msg.cmd,cmdBuf);
write(fd, &msg, sizeof(msg));
}
break;
case PUT:
fdfile = open(getDesDir(msg.cmd),O_RDWR|O_CREAT,0666);
write(fdfile, msg.sendBuf, strlen(msg.sendBuf));
close(fdfile);
break;
case QUIT:
printf("client quit!");
exit(-1);
}
}
int main(int argc, char **argv)
{
int s_fd;
int c_fd;
int n_read;
struct Msg msg;
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
if(argc !=3){
printf("param is not good\n");
exit(-1);
}
memset(&s_addr, 0, sizeof(struct sockaddr_in));
memset(&c_addr, 0, sizeof(struct sockaddr_in));
s_fd = socket(AF_INET, SOCK_STREAM, 0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
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));
listen(s_fd, 10);
int clen = sizeof(struct sockaddr_in);
printf("Waiting for client conneciton...\n");
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));
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);
}
}
}
}
close(c_fd);
close(s_fd);
return 0;
}
client客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"
char *getDir(char *cmd)
{
char *p;
p = strtok(cmd," ");
p = strtok(NULL," ");
return p;
}
int get_cmd_type(char *cmd)
{
if(!strcmp(cmd, "ls")){ return LS;}
if(!strcmp(cmd, "pwd")){ return PWD;}
if(!strcmp(cmd, "lls")){ return LLS;}
if(!strcmp(cmd, "quit")){ return QUIT;}
if(strstr(cmd, "lcd")){ return LCD;}
if(strstr(cmd, "cd")){ return CD;}
if(strstr(cmd, "put")){ return PUT;}
if(strstr(cmd, "get")){ return GET;}
return -1;
}
int cmd_handler(struct Msg msg, int fd)
{
char buf[32] = {0};
char *dir = NULL;
int filefd;
int ret = get_cmd_type(msg.cmd);
switch(ret){
case LS:
case CD:
case PWD:
msg.type = 0;
write(fd, &msg, sizeof(msg));
break;
case GET:
msg.type = 2;
write(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.sendBuf, sizeof(msg.sendBuf));
close(filefd);
write(fd, &msg, sizeof(msg));
}
break;
case LLS:
printf("——————————————————————————————————————\n");
system("ls");
printf("——————————————————————————————————————\n");
break;
case LCD:
dir = getDir(msg.cmd);
chdir(dir);
break;
case QUIT:
strcpy(msg.cmd, "client quit!");
write(fd, &msg, sizeof(msg));
close(fd);
exit(-1);
}
return ret;
}
void handler_server_message(struct Msg msg, int fd)
{
int n_read;
struct Msg msgGet;
int newfilefd;
n_read = read(fd, &msgGet, sizeof(msgGet));
if(n_read == 0){
printf("server is out! quit\n");
exit(-1);
}else if(msgGet.type == DOFILE){
char *p = getDir(msg.cmd);
newfilefd = open(p, O_RDWR|O_CREAT,0600);
write(newfilefd, 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 n_read;
struct Msg msg;
struct sockaddr_in c_addr;
int mark = 0;
if(argc !=3){
printf("param is not good\n");
exit(-1);
}
memset(&c_addr, 0, sizeof(struct sockaddr_in));
c_fd = socket(AF_INET, SOCK_STREAM, 0);
if(c_fd == -1){
perror("socket");
exit(-1);
}
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(struct sockaddr)) == -1){
perror("connect");
exit(-1);
}
printf("connect ....\n");
while(1){
memset(msg.cmd, 0, sizeof(msg.cmd));
if(mark == 0){
printf(">");
}
gets(msg.cmd);
if(strlen(msg.cmd) == 0){
if(mark == 1){
printf(">");
}
continue;
}
mark = 1;
int ret = cmd_handler(msg,c_fd);
if( ret > IFGO){
printf(">");
fflush(stdout);
continue;
}
if( ret == -1){
printf("command not\n");
printf(">");
fflush(stdout);
continue;
}
handler_server_message(msg,c_fd);
}
close(c_fd);
return 0;
}