客户端将手机号发送给服务器,服务器将包含该手机号文件名发送给客户端

来源:互联网 发布:云狐网络科技园 编辑:程序博客网 时间:2024/05/27 19:26

客户端

vi grep_client.c

#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <string.h>#include <dirent.h>#include <unistd.h>#include <time.h>#define MYPORT 10009#define HISPORT 10010 #define IP_COUNT 13// 2011-09-18// msn: xorg@163.com// client: ver0.1struct packet {        char data[1024];};void query_log(char *ip,char *phone) {        int sd,sd2,newsd,ret;        struct packet pkt;        struct sockaddr_in my_end,his_end;        int his_end_len;        time_t timep;        struct tm *tp;        char nowtime[128];        pid_t pid;        timep=time(NULL);        tp=localtime(&timep);        sprintf(nowtime,"%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);        printf("%s\n",nowtime);        pid=fork();        if(pid==-1) {                fprintf(stderr,"fork() error.\n");                exit(-1);        }        if(pid==0) {                //-- send phone number                sd2=socket(PF_INET,SOCK_STREAM,0);                if(sd==-1) {                        fprintf(stderr,"socket error.\n");                        exit(-1);                }                his_end.sin_family=AF_INET;                his_end.sin_port=htons(HISPORT);                //his_end.sin_addr.s_addr=inet_addr("10.199.75.169");                his_end.sin_addr.s_addr=inet_addr(ip);                ret=connect(sd2,(struct sockaddr *)&his_end,sizeof(his_end));                if(ret==-1) {                        perror("connect()");                        exit(-1);                }                ret=write(sd2,phone,strlen(phone));                if(ret==-1) {                        fprintf(stderr,"write error.\n");                        exit(-1);                }                printf("client write %d bytes.\n\n",ret);                close(sd);        //      printf("\n\n");                exit(0);        }        else {        //-- receve file name                sd=socket(PF_INET,SOCK_STREAM,0);                if(sd==-1) {                        fprintf(stderr,"socket error.\n");                        exit(-1);                }                my_end.sin_family=AF_INET;                my_end.sin_port=htons(MYPORT);                my_end.sin_addr.s_addr=inet_addr("0.0.0.0");                int flag=1,len=sizeof(int);                 ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);                if(ret==-1) {                        fprintf(stderr,"setsockopt() error.\n");                }                ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));                if(ret==-1) {                        fprintf(stderr,"bind() error.\n");                        exit(-1);                }                ret=listen(sd,5);                if(ret==-1) {                        fprintf(stderr,"listen() error.\n");                        exit(-1);                }                his_end_len=sizeof(his_end);                while(1) {                        newsd=accept(sd,(struct sockaddr *)&his_end,&his_end_len);                        if(newsd==-1) {                                perror("accept()");                                exit(-1);                        }                        ret=read(newsd,&pkt,sizeof(pkt));                        if(ret==-1) {                                fprintf(stderr,"read error.\n");                                exit(-1);                        }                        (pkt.data)[ret]='\0';                //      printf("server read %d bytes, recv=\n%s\n",ret,pkt.data);                        if(strcmp(ip,"10.199.81.164"))                                printf("server read %d bytes:\n%s\n\n",ret,pkt.data);                        else                                printf("server read %d bytes:\n%s\n",ret,pkt.data);                        ret=close(newsd);                        if(ret==-1) {                                fprintf(stderr,"close() error.\n");                                close(sd);                                exit(-1);                        }                        // exit process                        ret=close(sd);                        if(ret==-1) {                                fprintf(stderr,"close() error.\n");                                exit(-1);                        }                        return;                }        }}int main(int argc,char *argv[]) {        int i;        char *array_ip[IP_COUNT]={"10.199.81.152","10.199.81.153","10.199.81.154","10.199.81.155","10.199.81.156","10.199.81.157","10.199.81.158","10.199.81.159","10.199.81.160","10.199.81.161","10.199.81.162","10.199.81.163","10.199.81.164"};        if(argc<2) {                printf("%s phone_num\n",argv[0]);                exit(-1);        }        for(i=0;i<IP_COUNT;i++)                query_log(array_ip[i],argv[1]);        exit(0);}

 

服务端

vi grep_server-2011-09-21.c

#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <time.h>#define MYPORT 8800#define HISPORT 10009#define LOGFILE "/home/mtvwap/file.log"//#define LOGFILE "/home/uniadmin/test/logfile.log"// 2011-09-18// msn: xorg@163.com// server: ver0.1struct packet {        char data[64];};int main() {        FILE *file,*fp_log;        char cmd[512];        char line[1024];        char data[2048];        char hostname[128];        time_t timep;        struct tm *tp;        char nowtime[128];        //--/*        pid_t pid;        int fd;        pid=fork();        if(pid==-1) {                perror("fork()");                exit(-1);        }        if(pid!=0)                exit(0);        else {                fd=open("/dev/null",O_RDONLY);                if(fd==-1) {                        perror("open()");                } else {                        close(STDIN_FILENO);                        dup(fd);                        close(fd);                }                fd=open("/dev/null",O_WRONLY);                if(fd==-1) {                        perror("open()");                } else {                        close(STDOUT_FILENO);                        dup(fd);                        close(STDERR_FILENO);                        dup(fd);                        close(fd);                }                setsid();                chdir("/");        }*/        //--        gethostname(hostname,sizeof(hostname));        printf("%s\n",hostname);        int sd,sd2,newsd,ret;        struct sockaddr_in my_end,his_end;        int his_end_len;        struct packet pkt;        sd=socket(PF_INET,SOCK_STREAM,0);        if(sd==-1) {                fprintf(stderr,"socket error.\n");        //      exit(-1);        }        // my_end        my_end.sin_family=AF_INET;        my_end.sin_port=htons(MYPORT);        my_end.sin_addr.s_addr=inet_addr("0.0.0.0");        //      my_end.sin_addr.s_add=INADDR_ANY;        int flag=1,len=sizeof(int);        ret=setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,len);        if(ret==-1) {                fprintf(stderr,"setsockopt() error.\n");        }                ret=bind(sd,(struct sockaddr *)&my_end,sizeof(my_end));        if(sd==-1) {                fprintf(stderr,"socket error.\n");                exit(-1);        }        ret=listen(sd,5);        if(ret==-1) {                fprintf(stderr,"sd listen error.\n");                fprintf(fp_log,"%s","listen() error.\n");                exit(-1);        }        his_end_len=sizeof(his_end);        while(1) {                newsd=accept(sd,(struct sockaddr *)&his_end,&his_end_len);                if(newsd==-1) {                        perror("accept()");                //      exit(-1);                }                printf("accept ok\n");                printf("his_end=%s\n",inet_ntoa(his_end.sin_addr));                ret=read(newsd,&pkt,sizeof(pkt));                if(ret==-1) {                        fprintf(stderr,"read error.\n");                        fprintf(fp_log,"%s","newsd read() error.\n");                //      exit(-1);                }                //else continue;                (pkt.data)[ret]='\0';                printf("read newsd ok\n");                // log file                fp_log=fopen(LOGFILE,"a+");                if(fp_log==NULL) {                        fprintf(stderr,"fopen() error.\n");                       // exit(-1);                }                printf("fopen ok\n");                timep=time(NULL);                tp=localtime(&timep);                sprintf(nowtime,"\n\n%d-%02d-%02d-%02d-%02d-%02d",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);                fprintf(fp_log,"%s\n",nowtime);                fprintf(fp_log,"%s\n",hostname);                printf("server read %d bytes, recv=%s\n",ret,pkt.data);                fprintf(fp_log,"server read %d bytes, recv=%s\n",ret,pkt.data);                // log file                // -----------                // sprintf(cmd,"%s","cd /home/mtvwap/portal_wap/logs/server ; grep -l 18635529831 server*");                sprintf(cmd,"cd /home/mtvwap/portal_wap/logs/server ; grep -l %s  server*",pkt.data);                printf("cmd=%s\n",cmd);                if(strstr(cmd,"\n")) {                        printf("warn:cmd is have enter, to restart.\n\n");                        continue;                }                file=popen(cmd,"r");                if(file!=NULL) {                        sprintf(data,"%s\n",hostname);                        while(fgets(line,1023,file)!=NULL) {                                // printf("%s",line);                                //      strcpy(data,hostname);                                strcat(data,line);                                //      strcat(data,"\n");                        }                }                strcat(data,"----------------");                printf("popen ok\n");                pclose(file);                printf("pclose ok\n");                // -----------                /*                   if(strstr(pkt.data,"hello"))                   printf("execute script.\n");                   else                   printf("no execute script.\n");                 */                //--                sleep(2);                sd2=socket(PF_INET,SOCK_STREAM,0);                if(sd==-1) {                        fprintf(stderr,"socket error.\n");                        fprintf(fp_log,"%s","sd2 socket() error.\n");                //      exit(-1);                }                printf("socket sd2 ok\n");                // his_end                his_end.sin_family=AF_INET;                his_end.sin_port=htons(HISPORT);        //      his_end.sin_addr.s_addr=inet_addr("10.199.81.166");                his_end.sin_addr.s_addr=his_end.sin_addr.s_addr;                ret=connect(sd2,(struct sockaddr *)&his_end,sizeof(his_end));                if(ret==-1) {                        perror("connect()");                        fprintf(fp_log,"%s\n","sd2 connect() error.");                        fclose(fp_log);                        printf("sd2 connect() stop, to restart.\n\n");                        continue;                //      exit(-1);                }                printf("connect ok\n");                ret=write(sd2,data,strlen(data));                if(ret==-1) {                        fprintf(stderr,"write error.\n");                        fprintf(fp_log,"%s\n","write error.");                //      exit(-1);                }                printf("write ok\n");                printf("\nclient write %d bytes .\nstr=%s\n",ret,data);                // log file                fprintf(fp_log,"client write %d bytes.\n",ret);                fprintf(fp_log,"%s\n\n","Ok!! grep success...");                ret=fclose(fp_log);                if(ret==EOF) {                        fprintf(stderr,"fclose() error.\n");                       // exit(-1);                }                printf("fclose ok\n");                // end log file                //--                printf("\n");                close(newsd);                close(sd2);        }        close(sd);        exit(0);}


 

原创粉丝点击