1、服务端
cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define bufsize 1024
int change(char cmd[128])
{
if(!strcmp("ls",cmd))
{
return 1;
}
else if(!strcmp("ps",cmd))
{
return 2;
}
else if(!strcmp("g",cmd))
{
return 3;
}
else if(strstr(cmd,"cd"))
{
return 4;
}
else if(strstr(cmd,"get"))
{
return 5;
}
else if(strstr(cmd,"put"))
{
return 6;
}
}
char *getbehind(char cmd[128])
{
char *p;
p = (char *)malloc(128);
p = strtok(cmd," ");
p = strtok(NULL," ");
return p;
}
void getmessage(char cmd[128],int c_fd)
{
char *p = getbehind(cmd);
int fd = open(p,O_RDWR|O_CREAT,0666);
char *readbuf = (char *)malloc(8000);
read(c_fd,readbuf,8000);
write(fd,readbuf,8000);
close(fd);
memset(readbuf,0,8000);
}
void putmessage(char cmd[128],int c_fd)
{
int fd =-1;
char *readbuf = (char *)malloc(8000);
char *p =(char *)malloc(8000);
readbuf = getbehind(cmd);
if(access(readbuf,F_OK) == -1)
{
printf("put file failed\n");
}
else
{
fd = open(readbuf,O_RDWR,0666);
read(fd,p,8000);
write(c_fd,p,8000);
printf("the file put success\n");
close(fd);
memset(p,0,8000);
}
}
void choosecmd(char* cmd,int c_fd)
{
int ret =-1;
FILE *fdb;
int fd;
char *freadbuf = (char *)malloc(8000);
char *readbuf = (char *)malloc(8000);
char *p = (char*)malloc(8000);
ret = change(cmd);
switch(ret)
{
case 1:
fdb = popen("ls","r");
fread(freadbuf,8000,1,fdb);
write(c_fd,freadbuf,8000);
memset(freadbuf,0,8000);
printf("ok\n");
break;
case 2:
system("ps");
break;
case 3:
read(c_fd,freadbuf,128);
printf("%s\n",freadbuf);
exit(1);
break;
case 4:
p = getbehind(cmd);
chdir(p);
memset(p,0,sizeof(p));
break;
case 5:
putmessage(cmd,c_fd);
break;
case 6:
getmessage(cmd,c_fd);
break;
}
}
int main(int argc,char** argv)
{
int c_fd = -1;
int s_fd =-1;
struct sockaddr_in c_addr;
if(argc!=3)
{
perror("argc\n");
exit(1);
}
//socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1)
{
perror("socket\n");
exit(1);
}
memset(&c_addr,0,sizeof(c_addr));
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&c_addr.sin_addr);
//bind
bind(s_fd,(struct sockaddr*)&c_addr,sizeof(c_addr));
//listen
listen(s_fd,10);
//accept
socklen_t clen = sizeof(c_addr);
int nread = 0;
char readbuf[1024];
printf("wait connecting\n");
memset(&c_addr,1,clen);
while(1)
{
c_fd = accept(s_fd,(struct sockaddr*)&c_addr,&clen);
if(c_fd == -1)
{
perror("accept");
exit(1);
}
printf("connect success!%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0)
{
while(1)
{
nread = read(c_fd,readbuf,128);
choosecmd(readbuf,c_fd);
memset(readbuf,0,sizeof(readbuf));
}
}
}
return 0;
}
2、客户端
cpp
#include<stdio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#define bufsize 1024
#define bufsize 1024
char *getbehind(char cmd[128])
{
char *p;
p = (char *)malloc(8000);
p = strtok(cmd," ");
p = strtok(NULL," ");
return p;
}
void getmessage(char cmd[128],int c_fd)
{
char *p = getbehind(cmd);
int fd = open(p,O_RDWR|O_CREAT,0666);
char *readbuf = (char *)malloc(8000);
read(c_fd,readbuf,8000);
write(fd,readbuf,8000);
close(fd);
memset(readbuf,0,8000);
}
void putmessage(char cmd[128],int c_fd)
{
int fd =-1;
char *readbuf = (char *)malloc(8000);
char *p =(char *)malloc(8000);
readbuf = getbehind(cmd);
if(access(readbuf,F_OK) == -1)
{
printf("put file failed\n");
}
else
{
fd = open(readbuf,O_RDWR,0666);
read(fd,p,8000);
write(c_fd,p,8000);
printf("the file put success\n");
close(fd);
memset(p,0,8000);
}
}
int change(char cmd[128])
{
if(!strcmp("ls",cmd))
{
return 1;
}
else if(!strcmp("lls",cmd))
{
return 2;
}
else if(!strcmp("g",cmd))
{
return 3;
}
else if(strstr(cmd,"cd"))
{
return 4;
}
else if(strstr(cmd,"lcd"))
{
return 5;
}
else if(strstr(cmd,"get"))
{
return 6;
}
else if(strstr(cmd,"put"))
{
return 7;
}
}
void choosecmd(char cmd[128],int c_fd)
{
int ret =-1;
char *p = (char*)malloc(8000);
int fd =-1;
char *readbuf =(char*)malloc(8000);
ret = change(cmd);
switch(ret)
{
case 1:
read(c_fd,p,8000);
printf("%s\n",p);
memset(p,0,8000);
break;
case 2:
system("ls");
break;
case 3:
printf("unconnecting\n");
write(c_fd,"away host",128);
close(c_fd);
exit(-1);
break;
case 4:
break;
case 5:
p = getbehind(cmd);
chdir(p);
memset(p,0,sizeof(p));
break;
case 6:
getmessage(cmd,c_fd);
break;
case 7:
putmessage(cmd,c_fd);
break;
}
}
int main(int argc,char** argv)
{
int c_fd = -1;
struct sockaddr_in c_addr;
if(argc!=3)
{
perror("argc\n");
exit(1);
}
//socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1)
{
perror("socket\n");
exit(1);
}
memset(&c_addr,0,sizeof(c_addr));
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&c_addr.sin_addr);
//connect
int clent = sizeof(c_addr);
if(connect(c_fd,(struct sockaddr*)&c_addr,clent) == -1)
{
perror("connect");
perror("why:");
exit(1);
}
//wait send
char writebuf[bufsize] = {0};
printf("connect .....\n");
while(1)
{
fgets(writebuf,1024,stdin);
writebuf[strcspn(writebuf, "\n")] = '\0';
printf("cmd:%s\n",writebuf);
write(c_fd,writebuf,strlen(writebuf));
choosecmd(writebuf,c_fd);
printf("....................cmd......................\n");
memset(writebuf,0,sizeof(writebuf));
}
return 0;