想写一个不同机器通信获取状态的c程序。遇到无数困难。断断续续了3、4周了,得到的结果仍然无法面世。 我想还是把其中遇到的所有困难写下来吧!
下面是结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 |
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 |
#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); } |
真是搞不懂,竟然在这个循环里面卡住了。
1 |
fp文件里面有三行, |
1
2
3
4 |
while (fgets(buffer, MAX_LINE, fp) != NULL) { printf( "buffer is %sqqq\n" ,buffer); }<br><br>。。<br>待续。。 |
原文:http://www.cnblogs.com/FlyAway2013/p/3608824.html