此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出。欢迎各位前来交流。(部分材料来源网络,若有侵权,立即删除)
本人博客所有文章纯属学习之用,不涉及商业利益。不合适引用,自当删除!
若被用于非法行为,与我本人无关
代码
服务端
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<string.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#define PORT 8900
#define BUF_SIZE 2048
#include<stdlib.h>
int execute(char* command,char* buf);
int main()
{
int sockfd;
struct sockaddr_in client;
struct sockaddr_in server;
char send_buf[BUF_SIZE];
char recv_buf[BUF_SIZE];
char cmd[4096];
int sendnum;
int recvnum;
int length;
int port;
int connected;
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
memset(cmd,0,4096);
port = PORT;
if (-1==(sockfd=socket(AF_INET,SOCK_STREAM,0))) //change
{
perror("[error]:socket connect failed\n");
exit(1);
}
memset(&server,0,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port);
if (-1==bind(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr)))
{
perror("[error]:binding\n");
close(sockfd);
exit(1);
}
if(-1==listen(sockfd,10))
{
perror("[error]:listien\n");
close(sockfd);
return -1;
}
length = sizeof(struct sockaddr_in);
while(1)
{
memset(recv_buf,0,2048);
memset(send_buf,0,2048);
if(-1==(connected=accept(sockfd,(struct sockaddr*)&client,&length)))
{
perror("[error]:three shakehands\n");
close(sockfd);
return -1;
}
//change///
int pid = fork();
if((pid=fork())<0)
{
printf("fork failed\n");
_exit(1);
}
else if(pid == 0)
{
process(connected, client);
}
else
{
close(connected);
continue;
}
close(connected);
}
close(sockfd);
exit(1);
}
int process(int connected, struct sockaddr_in client)
{
char sendbuf[BUF_SIZE];
char recvbuf[BUF_SIZE];
int sendnum = 0;
int recvnum = 0;
int cnt;
int n, fd[2];
int length = sizeof(struct sockaddr_in);
char cmd[10];
char path[2048];
pid_t pid;
memset(sendbuf, 0, BUF_SIZE);
memset(recvbuf, 0, BUF_SIZE);
if(pipe(fd)<0)
printf("[error]:creat pipe \n");
while(1)
{
if(0>=(recvnum=recv(connected, recvbuf, BUF_SIZE, 0)))
{
perror("[error]:recv\n");
close(connected);
return -1;
}
recvbuf[recvnum]='\0';
printf("子进程pid:%d \n父进程pid:%d\n连接描述符:%d\n",getpid(),getppid(),connected);
fprintf(stderr, "the command is:%s\n", recvbuf);
if(0==strcmp(recvbuf, "quit"))
{
perror("quitting remote controling\n");
close(connected);
return 0;
}
strcpy(cmd,"/bin/");
strcat(cmd,recvbuf);
execute(cmd,sendbuf);
if ('\0'==*sendbuf)
{
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"/sbin/");
strcat(cmd,recvbuf);
execute(cmd,sendbuf);
if ('\0'==*sendbuf)
{
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"/usr/bin/");
strcat(cmd,recvbuf);
execute(cmd,sendbuf);
}
if ('\0'==*sendbuf)
{
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"/usr/sbin/");
strcat(cmd,recvbuf);
execute(cmd,sendbuf);
}
}
if ('\0'==*sendbuf)
sprintf(sendbuf,"command is not vaild,check it please\n");
printf("%s\n",sendbuf);
sendnum = send(connected,sendbuf,sizeof(sendbuf),0);
if (0>sendnum)
{
perror("sending data error\n");
continue;
}
}
}
int execute(char* command,char* buf)
{
FILE * fp;
int count;
if (NULL==(fp = popen(command,"r")))
{
perror("[error]:creating pipe\n");
exit(1);
}
count = 0 ;
while(((buf[count] = fgetc(fp))!=EOF)&&count<2047)
count++;
buf[count]='\0';
pclose(fp);
if(0==count) //change
printf(buf, "%s cannot execute\n",command);
return count;
}
客户端
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<string.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 8900
#define BUF_SIZE 2048
void print_usage(char* str)
{
printf("%s usage :\n",str);
printf("%s Ipaddr \n");
}
int main(int argc, char** argv)
{
int sockfd;
struct sockaddr_in client;
struct sockaddr_in server;
char send_buf[BUF_SIZE];
char recv_buf[BUF_SIZE];
int sendnum;
int recvnum;
int length;
int port;
if(2!=argc)
{
print_usage(argv[0]);
exit(1);
}
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
port = PORT;
if (-1==(sockfd=socket(AF_INET,SOCK_STREAM,0))) // change
{
perror("generating socket error\n");
exit(1);
}
memset(&server,0,sizeof(struct sockaddr_in)); //change
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons(port);
if(-1==connect(sockfd, (struct sockaddr*)&server, sizeof(server))) //change
{
perror("connect error\n");
close(sockfd);
return -1;
}
while(1)
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
printf("tcp>"); //change
gets(send_buf); //change Don't use 'fgets'!!!!!!!!!!!!
printf("\n");
if (0>send(sockfd,send_buf,strlen(send_buf),0)) //change
{
perror("error occar in sending data\n");
break;
}
if (0==strcmp(send_buf,"quit"))
{
printf("quit...\n"); //change
break;
}
length = sizeof(struct sockaddr);
recvnum = recv(sockfd,recv_buf,2048,0);
if(0<recvnum) //change
{
recv_buf[recvnum]='\0';
printf("%s\n",recv_buf);
}
else{
continue;
}
//printf("%s\n",recv_buf);
}
close(sockfd);
exit(1);
}
运行效果
- 分别执行三个客户端进程:
- 查看服务器输出情况:
- 查看进程结束前后变化: