Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42639
  • 博文数量: 20
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-04 21:13
文章分类

全部博文(20)

文章存档

2011年(20)

我的朋友
最近访客

分类: C/C++

2011-08-25 20:18:02

  1. /*
  2. *客户端,执行命令,回传执行结果
  3. *By rekken
  4. */
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include <netinet/in.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #define SERVERPORT 3490
  14. #define BACKLOG 10
  15. #define MAX_CONNECTED_NO 10
  16. #define MAXDATASIZE 120
  17. int main()
  18. {
  19.         //declare the variable to be used
  20.     FILE *pp;;    //used for file handle, pp used as buf after doing command
  21.  
  22.     char sendout[65535];    //used for store the data retrived from the file
  23.     struct sockaddr_in server_sockaddr,client_sockaddr;    //for create socket
  24.     int sin_size,recvbytes,sendbytes;    //recv and send used for recv() and send()
  25.     int sockfd,client_fd;    //for socket creation
  26.     char buf[MAXDATASIZE];    //store the file dir from recv()
  27.     char output[MAXDATASIZE];
  28.         //creat socket connection
  29.     if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
  30.         perror("socket");
  31.         exit(1);
  32.     }
  33.     printf("socket success!,sockfd=%d\n",sockfd);
  34.  
  35.         //set relevant parameters in the socketr_in struct
  36.     server_sockaddr.sin_family=AF_INET;
  37.     server_sockaddr.sin_port=htons(SERVERPORT);
  38.     server_sockaddr.sin_addr.s_addr=INADDR_ANY;
  39.     bzero(&(server_sockaddr.sin_zero),8);
  40.  
  41.         //bind the sockfd and port
  42.     if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
  43.         perror("bind");
  44.         exit(1);
  45.     }
  46.     printf("bind success!\n");
  47.  
  48.         //begin to listen
  49.     if(listen(sockfd,BACKLOG)==-1){
  50.         perror("listen");
  51.         exit(1);
  52.     }
  53.     printf("listening....\n");
  54.  
  55.             //call the accept(),wait for the peer's connecting
  56.         if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
  57.             perror("accept");
  58.             exit(1);
  59.         }
  60.  
  61.             //receive the data from the peer
  62.         if((recvbytes=recv(client_fd,buf,sizeof(buf),0))==-1){
  63.             perror("recv");
  64.             exit(1);
  65.         }
  66.             //do the received command
  67.  
  68.         if((pp = popen(buf, "r")) == NULL )
  69.         {
  70.             printf("popen error!\n");
  71.             return 0;
  72.         }
  73.         while(fgets(output, 255, pp))
  74.         {
  75.             printf("%s", output);
  76.             if((sendbytes=send(client_fd,output,strlen(output),0))==-1){
  77.                 perror("send file");
  78.                 exit(1);
  79.             }
  80.             printf("send data length:%d\n",sendbytes);
  81.         }
  82.         pclose(pp);
  83.  
  84.         close(sockfd);
  85.         return 1;
  86. }

  1. /*
  2. *讲命令执行部分封装
  3. */

  4. int do_command(int sockfd,char *command,char *output)//sockfd套接字用于结果回传,command为待执行命令,output存放执行结果
  5. {
  6.     FILE *pp;
  7.     int count_byte=0;
  8.     if((pp = popen(command, "r")) == NULL )
  9.     {
  10.         printf("popen error!\n");
  11.         return 0;
  12.     }
  13.     while(!feof(pp)){//判断文件尾,下两行用于将文件内容保存在output中
  14.         output[count_byte]=fgetc(pp);
  15.         count_byte++;
  16.     }
  17.     if((send(sockfd,output,count_byte,0))==-1){//将执行结果发送回请求端
  18.         perror("send file");
  19.         exit(1);
  20.     }
  21.     pclose(pp);
  22.     return count_byte;//返回值为传送字节数
  23. }

阅读(1117) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~