想写一个不同机器通信获取状态的c程序。遇到无数困难。断断续续了3、4周了,得到的结果仍然无法面世。 我想还是把其中遇到的所有困难写下来吧!
下面是结果
#include#include #include #include #include #include #include #include #include #define SERV_PORT 1113#define LISTENQ 32#define MAXLINE 1024#define MAX_LINE 200/***连接处理函数***/void sendMSG2Client(int fd);int isUsing = 0; // 0没使用 1正在被使用char *who;char *productName;char* productVersion;char *path;char *setupMode;char *dbType;void split(char arr[], char *arr2[], const char *del) { int i = 0; printf("arr %s\n",arr); char *s = strtok(arr, "="); while(s != NULL) { arr2[i++] = s; printf("arr2 i %s\n",arr2[i]); s = strtok(NULL, "="); } //return arr2;}int readsystemcfgfile( char* filename ){ FILE* fp; char buffer[15]; fp = fopen(filename, "r"); if (fp == NULL) { perror("File open error"); exit(1); } char *arr[2]; while (fgets(buffer, MAX_LINE, fp) != NULL) { printf("buffer is %sqqq\n",buffer); } printf("aa %s",arr[1]); return 0;}/*截取src字符串中,从下标为start开始到end-1(end前面)的字符串保存在dest中(下标从0开始)*/char *substring(char *src,int start,int end){ printf("The buf is : %s\n" , src); int ss = 0; for ( ss=0 ; ss strlen(src))return; if(end>strlen(src)) end=strlen(src); while(i "); if(end == NULL ) { printf(" not he liee\n"); continue; } printf("end %s " , end); productVersion = substring(buffer,33,50); } } printf("productVersion is : %s",productVersion); return 0;}void sendMSG2Client(int sockfd){ ssize_t n; char buf[MAXLINE]; fprintf(stdout,"bbbb 4444 \n"); again: while ( (n = read(sockfd, buf, MAXLINE)) > 0) { printf(" buf is : %s\n" , buf); if( strstr(buf, "showStatus") ){// 查看 printf(" showStatus = : %s\n", buf); //write(sockfd, isUsing, 2); readsystemcfgfile("/opt/openssh/mytest.cfg"); readVersionfile("/opt/openssh/mytest.cfg"); char destination[75]; strcat(destination,productName); strcat(destination,path); strcat(destination,productVersion); strcat(destination,who); write(sockfd, destination, sizeof(destination)); }else if ( strcmp("register",buf)==0 ) {//注册 printf("register !"); isUsing = 1; who=buf; write(sockfd, "Register success!", n); printf("Register success!"); } else if ( strcmp("unregister",buf)==0 ) {//取消注册 who = NULL; printf("unregister !"); isUsing = 0; write(sockfd, "Unregister success!", strlen("Unregister success!")); } break; } if (n < 0 && errno == EINTR)//被中断,重入 goto again; else if (n < 0){//出错 fprintf(stderr,"read error:%s\n\a",strerror(errno)); // fprintf ?? exit(1); }}int main(int argc, char *argv[]){// printf失效? // strcat("Hi! I am " , ip_search()) ___ 这里却不报错。。。 怎么异常捕捉? write(sockfd, strcat("Hi! I am " , ip_search()), 10); int listenfd,connfd; pid_t childpid; socklen_t clilen; struct sockaddr_in servaddr; struct sockaddr_in cliaddr; if((listenfd = socket(AF_INET, SOCK_STREAM,0))==-1){ fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); exit(1); } /* 服务器端填充 sockaddr结构*/ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl (INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); /* 捆绑listenfd描述符 */ if(bind(listenfd,(struct sockaddr*)(&servaddr),sizeof(struct sockaddr))==-1){ fprintf(stderr,"Bind error:%s\n\a",strerror(errno)); exit(1); } /* 监听listenfd描述符*/ if(listen(listenfd,5)==-1){ fprintf(stderr,"Listen error:%s\n\a",strerror(errno)); exit(1); } for ( ; ; ) { clilen = sizeof(cliaddr); /* 服务器阻塞,直到客户程序建立连接 */ if((connfd=accept(listenfd,(struct sockaddr*)(&cliaddr),&clilen))==-1){ fprintf(stderr,"Accept error:%s\n\a",strerror(errno)); exit(1); } fprintf(stdout,"aaa \n"); //有客户端建立了连接后 if ( (childpid = fork()) == 0) { /*子进程*/ fprintf(stdout,"bbbb 1 \n"); close(listenfd); /* 关闭监听套接字*/ fprintf(stdout,"bbbb 2 \n"); sendMSG2Client(connfd); /*处理该客户端的请求*/ fprintf(stdout,"bbbb 3 \n"); exit (0); } close(connfd);/*父进程关闭连接套接字,继续等待其他连接的到来*/ }}
asd
#include#include #include #include #include #include #include #include #include #include #include #include #include #define SERV_PORT 1113#define MAXLINE 1024#define LISTENQ 32#define ERRORIP "cannot find host ip"char* sendMSG2Server(FILE *fp, char *cmd, char *ipaddr);char* servaddrs[] = {"10.xx.xx.118","10.xx.xx.117","10.xx.xx.116"};char *ip_search(void);// 第一个参数,操作类型,第二个参数ip,若ip为空,则register/unregister当前int main(int argc, char **argv) { char* ipaddr; char* ret; printf("argsss : %d" , argc); if ( argc < 2 || argc > 3){ fprintf(stderr,"Param error ! usage: operation \n\a"); exit(0); } printf("ffff cmd : $s",argv); // if (argv[1] == "showAllStatus") {dd//查看所有showAll if( strcmp(argv[1], "showAllStatus")==0 ){ int eng = 2;//servaddrs.length; int i; for(i=0;i 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr])); close(sfd); return inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr);}
真是搞不懂,竟然在这个循环里面卡住了。
fp文件里面有三行,
while (fgets(buffer, MAX_LINE, fp) != NULL) { printf("buffer is %sqqq\n",buffer); } 。。 待续。。
#include#include #include #include #include #include #include #include #include #define SERV_PORT 1113#define LISTENQ 32#define MAXLINE 1024#define MAX_LINE 200#define ARRLEN(arr) (sizeof(arr)/sizeof(arr[0]))/***连接处理函数***/void sendMSG2Client(int fd);int isUsing = 0; // 0没使用 1正在被使用char *who;char *productName;char productVersion[20];char *productPath;char *setupMode;char *dbType;void split(char src[], char *dest[], const char *del) { int i = 0; char *s = strtok(src, del); while(s != NULL) { dest[i++] = s; s = strtok(NULL, del); }}int readsystemcfgfile( ){ FILE* fp; char *filename = "/opt/openssh/system.cfg"; char buffer[50]; fp = fopen(filename, "r"); if (fp == NULL) { perror("File open error"); exit(1); } char *systemcfgArr[2] ; while (fgets(buffer, MAX_LINE, fp) != NULL) { if ( !strstr(buffer, "=" ) ) { continue; } split(buffer, systemcfgArr, "="); } productName = systemcfgArr[0]; productPath = systemcfgArr[1]; int i; for(i=0;i strlen(src))return; if(end>strlen(src)) end=strlen(src); while(i "); if(end == NULL ) { continue; } //char xx[] = "abcdefghijklmnopqrstuvwxyz123456789"; substring( buffer, productVersion , 33,59); //printf("end productVersion %s \n" ,productVersion); //return 0; break; } } //printf("productVersion is : %s",productVersion); return 0;}void sendMSG2Client(int sockfd){ ssize_t n; char buf[MAXLINE]; char *registerInfo[2]; //fprintf(stdout,"bbbb 4444 \n"); again: while ( (n = read(sockfd, buf, MAXLINE)) > 0) { //printf(" buf is : %s\n" , buf); int i; for(i=0;i
asd
#include#include #include #include #include #include #include #include #include #include #include #include #include #define SERV_PORT 1113#define MAXLINE 1024#define LISTENQ 32#define ERRORIP "cannot find host ip"char *sendMSG2Server(char **args, char *ipaddr);char* sendMSG2Server0(char *cmd, char *ipaddr);char* servaddrsaaa[] = {"10.67.164.218","10.67.164.217","10.67.164.216"};char* servaddrs[] = {"10.67.164.218","10.67.164.217"};char *ip_search(void);// 第一个参数,操作类型,第二个参数ip,若ip为空,则register/unregister当前int main(int argc, char **argv) { char* ipaddr; printf("argc : %d\n" , argc); if ( argc < 2 || argc > 3){ fprintf(stderr,"Param error ! usage: operation + \n\a"); exit(0); } printf("argv: $s\n",argv); char ret[2000]; char *retLine; if (checkParam(argc, argv) == 0) { return -1; } char destination[100] = ""; sprintf(destination, "%10s%15s%30s%45s\n", "WHO" ,"product Name" , "product Path" , "product Version"); printf(destination); if( strcmp(argv[1], "sa")==0 ){//查看所有showAll int eng = 2;//servaddrs.length; int i; for(i=0;i = 2) { if(strcmp(argv[1], "r") ==0 || strcmp(argv[1], "u")==0) { printf("argv[2] i is : %s \n", argv[2] ); ipaddr = ip_search(); } else { ipaddr = argv[2]; } } printf("Client ip is : %s \n", ipaddr); retLine = sendMSG2Server(argv, ipaddr); printf(retLine); } fprintf(stdout,"succ returned \n"); exit(0);}// 1 true; 0 falseint checkParam(int argc, char **argv) { if(strcmp(argv[1], "s") !=0 && strcmp(argv[1], "sa")!=0 && strcmp(argv[1], "r")!=0 && strcmp(argv[1], "u")!=0) { fprintf(stderr,"Param error ! usage: showAllStatus \n\a"); return 0; } if( strcmp(argv[1], "sa")==0 ){ //查看所有showAll if( argc > 2) { fprintf(stderr,"Param error ! usage: showAllStatus \n\a"); return 0; } } else { } return 1; }char * sendMSG2Server(char **argv, char *ipaddr) { char cmd[20]; char *opt = argv[1]; char *who; //strcat(cmd, opt); if (strcmp(opt, "s") ==0){// 查看 showStatus return sendMSG2Server0("showStatus", ipaddr);// wirte信息应该包括: 操作类型、操作人、日期、参数可以不需要 } else if(strcmp(opt, "r") ==0) {//注册 /*if( argc != 3) { fprintf(stderr,"Param error ! "); }*/ //who = argv[2]; //strcat(cmd, who); sprintf(cmd, "%s,%s", "register",argv[2]); return sendMSG2Server0(cmd, ipaddr); } else if (strcmp(opt, "unregister") == 0) {//取消注册 return sendMSG2Server0("unregister", ipaddr); }}char* sendMSG2Server110(char *cmd, char *ipaddr) { printf("aaaaaa %s\n", cmd); return "";} // 进行服务端的操作char* sendMSG2Server0(char *cmd, char *ipaddr) { int nbytes=0; int sockfd; struct sockaddr_in servaddr; char recvline[MAXLINE]; if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){ fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); exit(1); } /* 客户程序填充服务端的资料*/ bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SERV_PORT); if (inet_pton(AF_INET, ipaddr, &servaddr.sin_addr) <= 0){ fprintf(stderr,"inet_pton Error:%s\a\n",strerror(errno)); exit(1); } /* 客户程序发起连接请求*/ if(connect(sockfd,(struct sockaddr *)(&servaddr),sizeof(struct sockaddr))==-1){ fprintf(stderr,"connect Error:%s\a\n",strerror(errno)); exit(1); } write(sockfd, cmd, strlen(cmd)); if ((nbytes=read(sockfd, recvline, MAXLINE)) == 0){//从sockfd读取从服务器发来的数据 fprintf(stderr,"sendMSG2Server: server terminated prematurely\n"); exit(1); } recvline[nbytes]='\0'; //fputs(recvline, stdout); return recvline;}// 获取客户端 ip-- shell客户端ip .... 当前机器ip ? nochar *ip_search(void) { int sfd, intr; struct ifreq buf[16]; struct ifconf ifc; sfd = socket (AF_INET, SOCK_DGRAM, 0); if (sfd < 0) return ERRORIP; ifc.ifc_len = sizeof(buf); ifc.ifc_buf = (caddr_t)buf; if (ioctl(sfd, SIOCGIFCONF, (char *)&ifc)) return ERRORIP; intr = ifc.ifc_len / sizeof(struct ifreq); while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr])); close(sfd); return inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr);}