AIX shell

  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <arpa/inet.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <netinet/in.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include "command.h"
  12. #include "config.h"
  13. #define BUFFSIZE 50
  14. #define RESSIZE 512
  15. #define MAXPENDING 5
  16. #define FILE_NAME_MAX_SIZE 512
  17.     void Die(char *mess) { perror(mess); exit(1); }
  18.     void initsql()
  19.     {
  20.     }
  21.     void HandleClient(int new_server_socket) 
  22.     {
  23.         int fd;//filehandle
  24.         int fcount;//fileread
  25.         FILE *fp;
  26.         int received = -1;
  27.         if ( new_server_socket < 0)
  28.         {
  29.             printf("Server Accept Failed!/n");
  30.             return;
  31.         }
  32.         char buffer[BUFFSIZE];
  33.         char Response[RESSIZE];
  34.         bzero(buffer, BUFFSIZE);
  35.         bzero(Response, RESSIZE);
  36.         //发送和接受缓冲
  37.         while (received = recv(new_server_socket,buffer,BUFFSIZE,0)>0)
  38.         {
  39.         char *cmd;
  40.         char *par;
  41.         char *args;
  42.         cmd=strtok(buffer,splitcmd);
  43.         par=strtok(NULL,splitcmd);
  44.         //命令用空格分开
  45.         printf("cmd:%s/n",cmd);
  46.         printf("par:%s/n",par);
  47.         //显示命令
  48.         
  49.         if (strcmp(cmd,getcons)==0)//请求连接串
  50.         {
  51.                 printf("client reqconnection string/n");
  52.                 strcpy(Response,connstr);
  53.                 if((send(new_server_socket,Response,RESSIZE,0))!=RESSIZE)
  54.                 {
  55.                     printf("Response Faild %s/n", connstr);
  56.                     break;
  57.                 }
  58.         }
  59.         else if(strcmp(cmd,getfile)==0)//请求文件
  60.         {
  61.             fd = open(par,O_RDWR);
  62.             if(fd < 0)  Die("can't open the file:%s/n");
  63.             while(fcount = read(fd,Response,RESSIZE))
  64.             {
  65.                   write(new_server_socket,Response,fcount);
  66.             }
  67.             //fclose(fd);
  68.         }
  69.         else if (strcmp(cmd,cmdexit)==0)//请求关闭
  70.         {
  71.             close(new_server_socket);
  72.         }
  73.         else if (strcmp(cmd,cmdexec)==0)//执行命令
  74.         {
  75.             system(par);
  76.             /*fp=popen(par,"r");
  77.             while(fcount = read(fp,Response,RESSIZE))
  78.             {
  79.                   write(new_server_socket,Response,fcount);
  80.             }
  81.             pclose(fp);
  82.             */
  83.             //args=strtok(par,splitcmd) ;
  84.             //if (execlp(args,strtok(NULL,splitcmd),(char *)0)<0)
  85.             //{
  86.             //  send(new_server_socket,par,RESSIZE,0);
  87.             //}
  88.             //else
  89.             //{
  90.             //  printf("Client Exec:%s/n",par);
  91.             //}
  92.         }
  93.         else 
  94.         {
  95.             printf("nop/n");
  96.             send(new_server_socket,cmd,RESSIZE,0);
  97.         }
  98.         bzero(buffer, BUFFSIZE);                          
  99.         bzero(cmd, strlen(cmd));
  100.         bzero(par,strlen(par));
  101.         bzero(Response,RESSIZE);
  102.         }
  103.         
  104. }
  105.     int main(int argc, char *argv[])
  106.     {
  107.         int sock_send,sock_listen;  //新来的sock
  108.         struct sockaddr_in opluserver,localend;    //远程opluserver,sock_addr
  109.         char rip;
  110.         int rport;
  111.         char buffer[BUFFSIZE];
  112.         unsigned int echolen;
  113.         int received = 0;
  114.         if (argc != 2) 
  115.         {
  116.             fprintf(stderr, "USAGE: xoplus <port>/n");
  117.             exit(1);
  118.         }
  119.         
  120.         if ((sock_listen= socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
  121.         {
  122.             Die("Can't open listen port");
  123.             exit(1);
  124.         }
  125.         //创建侦听socket
  126.         memset(&localend,0,sizeof(localend));
  127.         localend.sin_family = AF_INET;                  
  128.         localend.sin_addr.s_addr = htonl(INADDR_ANY);   
  129.         localend.sin_port = htons(atoi(argv[1]));
  130.         printf("port%s /n",argv[1]);
  131.         //准备本地socket
  132.         if ((bind(sock_listen,(struct sockaddr *)&localend,sizeof(localend)))<0)
  133.         {
  134.             Die("Bind Port to localend faild/n");
  135.         }
  136.         //绑定端口号
  137.         if (listen(sock_listen,MAXPENDING)<0)
  138.         {
  139.             Die("Can't Listen on port");
  140.         }
  141.         //启动侦听
  142.         while (1)
  143.         {    
  144.            unsigned int clientlen = sizeof(opluserver);
  145.             if ((sock_send = accept(sock_listen, (struct sockaddr *) &opluserver, &clientlen)) < 0) 
  146.                 {
  147.                  Die("Failed to accept client connection");
  148.                  }
  149.           fprintf(stdout, "Client connected: %s/n",  inet_ntoa(opluserver.sin_addr));
  150.           HandleClient(sock_send);
  151.         }
  152.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值