想写一个不同机器通信获取状态的c程序。遇到无数困难。断断续续了3、4周了,得到的结果仍然无法面世。 我想还是把其中遇到的所有困难写下来吧!
下面是结果
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#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); ss++) {
printf("%c",src[ss]);
}
char *dest;
char *p=src;
int i = start;
if(start>strlen(src))return;
if(end>strlen(src))
end=strlen(src);
while(i<end)
{
dest[i-start]=src[i];
printf("%c",src[i]);
i++;
}
dest[i-start]='\0';
printf("\nThe dest is : %s\n" , dest);
return dest;
} int readVersionfile( char* filename )
{
FILE* fp;
char buffer[MAX_LINE];
//char* productVersion; filename[180] = "/opt/luo/c/test";
char *prodversionfile = strcat( filename, "/etc/version.xml");
fp = fopen(filename, "r");
if (fp == NULL) {
perror("File open error");
exit(1);
}
while (fgets(buffer, MAX_LINE, fp) != NULL) {
fputs(buffer, stdout);
char *start = strstr(buffer,"PLATFORM_VERSION");
printf("start %s " , start);
if ( start != NULL) {
char *end = strstr(buffer,"</param>");
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 <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <time.h> #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 <IPaddress>\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<eng;i++) {
ret = strcat(ret, sendMSG2Server( NULL, "showStatus", servaddrs[i]) );
}
} else {
printf("ip_search i is : %s \n",ip_search());
if( argc == 2) {
ipaddr = ip_search();
} else {
ipaddr = argv[2];
}
printf("Client ip is : %s", ipaddr);
ret = sendMSG2Server(stdin, argv[1], ipaddr);
}
printf(ret);
exit(0);
} // 进行服务端的操作
char* sendMSG2Server(FILE *fp, 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);
}
if( strcmp(cmd,"showStatus")==0 ){// 查看 showStatusa
printf("aaaaaa shhh \n ");
write(sockfd, "showStatus", strlen("showStatus")); //wirte信息应该包括: 操作类型、操作人、日期、参数可以不需要
} else if (cmd == "register") {//注册
write(sockfd, "register", strlen("register"));
} else if (cmd == "unregister") {//取消注册
write(sockfd, "unregister", strlen("unregister"));
}
fprintf(stdout,"AAAAAAAA \n");
if ((nbytes=read(sockfd, recvline, MAXLINE)) == 0){//从sockfd读取从服务器发来的数据
fprintf(stderr,"sendMSG2Server: server terminated prematurely\n");
exit(1);
}
fprintf(stdout,"bbbb \n");
recvline[nbytes]='\0';
fprintf(stdout,"cc \n");
fputs(recvline, stdout);
return recvline;
} // 获取客户端 ip-- shell客户端ip .... 当前机器ip ? no
char *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);
}
真是搞不懂,竟然在这个循环里面卡住了。
fp文件里面有三行,
while (fgets(buffer, MAX_LINE, fp) != NULL) {
printf("buffer is %sqqq\n",buffer); } 。。
待续。。
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#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(productName);i++) {
//printf(" x = : %c\n", productName[i]);
}
for(i=0;i<strlen(productPath) && productPath[i] != '\n' && productPath[i] != '\r' ;i++) {
//printf(" y = : %c\n", productPath[i]);
}
return 0;
} void substring(char *src,char *dest,int start,int end)
{
int i=start;
if(start>strlen(src))return;
if(end>strlen(src))
end=strlen(src);
while(i<end)
{
dest[i-start]=src[i];
i++;
}
dest[i-start]='\0';
return;
} int readVersionfile( char* filepath )
{
FILE* fp;
char buffer[MAX_LINE];
//char* productVersion;
// filepath[180] = "/opt/iEMP/iEMP";
char prodversionfile[100];
strcat( prodversionfile,filepath);
strcat( prodversionfile, "/etc/platformversion.xml");
fp = fopen(prodversionfile, "r");
if (fp == NULL) {
perror("File open error");
exit(1);
}
while (fgets(buffer, MAX_LINE, fp) != NULL) {
//fputs(buffer, stdout);
//printf(" version file = : %s\n", buffer);
char *start = strstr(buffer,"PLATFORM_VERSION");
//printf("start %s " , start);
if ( start != NULL) {
char *end = strstr(buffer,"</param>");
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<strlen(buf);i++) {
//printf(" buf x = : %c\n", buf[i]);
} if( strstr(buf, "showStatus") ){// 查看
//printf(" showStatus = : %s\n", buf);
//write(sockfd, isUsing, 2);
readsystemcfgfile();
char destination[100] = "";
char productNameArr[10] = "";
char productPathArr[20] = "";
int i;
for(i=0;i<strlen(productName);i++) {
productNameArr[i] = productName[i];
}
for( i=0;i<strlen(productPath);i++) {
if( productPath[i] != '\n' && productPath[i] != '\r' ) productPathArr[i] = productPath[i];
}
readVersionfile(productPathArr);
//printf(" who : %s\n" , who);
if( who==NULL) {
who = "NO ONE";
}
sprintf(destination, "%10s%15s%30s%45s\n", who, productNameArr , productPathArr , productVersion);
//printf(destination);
write(sockfd, destination, sizeof(destination)/sizeof(destination[0])); productName = "iemppppp";
//strcat(productVersion,productPath);
//char *pv =
//strcat(productVersion, productName);
} else if (strstr(buf, "register")) {//注册
printf("register rr : %s !\n", buf);
//printf(" aaaa ");
char src[MAXLINE];
//printf(" bbb ");
for(i=0;i<strlen(buf) && buf[i] != '\0' && buf[i] != '\r';i++) {
//printf(" aasdfff ");
src[i] = buf[i];
}
//printf(" aasdfff ");
split(src, registerInfo, ",");
char *opt = registerInfo[1];
char *whoaa;
for(i=0;i<8 && registerInfo[1][i] != '\0' && registerInfo[1][i] != ' ';i++) {
//printf("/%c ", registerInfo[1][i]);
whoaa[i] = registerInfo[1][i];
}
who = whoaa;//当前函数退出后, lk 也会被回收吗??
isUsing = 1;
write(sockfd, "Register success!", n);
} else if (strstr(buf, "unregister")) {//取消注册
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 mainaa(int argc,char * agrs[]) { char destination[50] = "succc";
//strcat(destination,"luo");
printf(" ****straa**** %s\n", destination); char buf[] = "asfdafregister,lk";
if (strstr(buf, "register")) { printf(" ****xx**** %s\n", destination);
} } 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 = vfork()) == 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 <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <time.h> #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 + <IPaddress>\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<eng;i++) {
sprintf(retLine, "%15s %s\n",servaddrs[i], sendMSG2Server0("showStatus", servaddrs[i]) );
strcat(ret,retLine);
}
printf(ret);
} else {
if( argc >= 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 false
int 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 ? no
char *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);
}