- 论坛徽章:
- 0
|
统计某站点的在线流量信息,telnet 60.2.251.8 后,会显示出该站的"上线人数XXXXX",我想取出这个XXXXX,以供某一程序使用
我写了一个:思路是连接后,strstr匹配"["字串,如图也确实有一个"[",可程序找不到,why??
- /********头文件**********/
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/uio.h>
- #include <unistd.h>
- #include <strings.h>
- #include <netinet/in.h>
- /********宏定义***********/
- #define MAXLINE 4096
- int
- main(int argc, char **argv)
- {
- int sockfd, n;
- char recvline[1+MAXLINE];
- struct sockaddr_in servaddr;
- void usage(void);
- if(2!=argc){
- usage();
- exit(-1);
- }
- if(0>(sockfd=socket(AF_INET,SOCK_STREAM,0))){
- fprintf(stderr,"Socket Error");
- abort();
- }
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_port=htons(23); /*telnet*/
- if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0){
- fprintf(stderr,"inet_pton function error for %s",argv[1]);
- abort();
- }
- if(connect(sockfd,(struct socket_addr *)&servaddr,sizeof(servaddr))<0){
- fprintf(stderr,"connect function error");
- abort();
- }
- bzero(&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_port=htons(23); /*telnet*/
- if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0){
- fprintf(stderr,"inet_pton function error for %s",argv[1]);
- abort();
- }
- if(connect(sockfd,(struct socket_addr *)&servaddr,sizeof(servaddr))<0){
- fprintf(stderr,"connect function error");
- abort();
- }
- n=read(sockfd,recvline,MAXLINE);
- recvline[n]=0; /*null terminate */
- char * locate=NULL;
- if( (locate=strstr(recvline,"[")) != NULL ){
- int num=0,r=1;
- fprintf(stdout,"%s==>%d\n",locate,num);
- for(--locate; *locate>='0' && *locate <='9'; --locate){
- num += *locate*r;
- r*=10;
- }
- fprintf(stdout,"%s==>%d",locate,num);
- exit(num);
- }
- exit(0);
- }
复制代码
![]() |
|