select实现telnet目标端口功能

vi interface_check.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/select.h>
#include <time.h>

#define INTF_NUM 5

#define VAC_SOAP_IP "10.199.69.33"
#define VAC_SOAP_PORT 9000

#define VAC_ORDER_IP "10.199.69.12"
#define VAC_ORDER_PORT 9999 

#define SGIP_IP "10.199.70.151"
#define SGIP_PORT 8881

#define CMS_FTP_IP "10.199.76.12"
#define CMS_FTP_PORT 21

#define CONTENT_SYNC_TO_CMS_IP "10.199.76.14"
#define CONTENT_SYNC_TO_CMS_PORT 8080

int send_to_169(char *snd_buf_tmp) {
        int sd;
        int ret;

        struct sockaddr_in his_end;

        sd=socket(PF_INET,SOCK_STREAM,0);
        if(sd==-1) {
                fprintf(stderr,"send_to_169 socket() error.\n");
                return -1;
        }

        his_end.sin_family=AF_INET;
        his_end.sin_port=htons(10010);
        his_end.sin_addr.s_addr=inet_addr("10.199.75.169");

        ret=connect(sd,(struct sockaddr *)&his_end,sizeof(his_end));
        if(ret==-1) {
                perror("send_to_169 connect()");
                return -1;
        }

        ret=write(sd,snd_buf_tmp,strlen(snd_buf_tmp));
        if(ret==-1) {
                fprintf(stderr,"send_to_169 write() error.\n");
                return -1;
        }

        printf("client write %d bytes.\n",ret);

        ret=close(sd);
        if(ret==-1) {
                fprintf(stderr,"send_to_169 close() error.\n");
                exit(-1);
        }

        return 0;
}

int main(void) {
        int sd;
        int ret;
        struct sockaddr_in his_end;
        int flags,flags1;

        int fdmax=0;
        fd_set rfds,wfds,exfds;
        struct timeval tv;

        int i;

        char snd_buf[1024];
//      char *ip[INTF_NUM]={"10.199.69.33","10.199.69.12","10.199.70.151","10.199.76.12","10.199.76.14"};
//      int port[INTF_NUM]={9000,9999,8881,21,8080};
//
        char *ip[INTF_NUM]={VAC_SOAP_IP,VAC_ORDER_IP,SGIP_IP,CMS_FTP_IP,CONTENT_SYNC_TO_CMS_IP};
        int port[INTF_NUM]={VAC_SOAP_PORT,VAC_ORDER_PORT,SGIP_PORT,CMS_FTP_PORT,CONTENT_SYNC_TO_CMS_PORT};
//
//      char *ip[INTF_NUM]={VAC_SOAP_IP};
//      int port[INTF_NUM]={VAC_SOAP_PORT};

        static char *ip_name[INTF_NUM]={"VAC_SOAP_IP","VAC_ORDER_IP","SGIP_IP","CMS_FTP_IP","CONTENT_SYNC_TO_CMS_IP"};
        static char *port_name[INTF_NUM]={"VAC_SOAP_PORT","VAC_ORDER_PORT","SGIP_PORT","CMS_FTP_PORT","CONTENT_SYNC_TO_CMS_PORT"};

//      static char *ip_name[INTF_NUM]={"VAC_SOAP_IP"};
//      static char *port_name[INTF_NUM]={"VAC_SOAP_PORT"};

        char nowtime[128];
        char hname[128],hostname[128];
        char *p,*h_name;

        time_t timep;
        struct tm *p1;

        timep=time(NULL);
        p1=localtime(&timep);
        snprintf(nowtime,128,"%d-%02d-%02d-%02d-%02d-%02d",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday,p1->tm_hour,p1->tm_min,p1->tm_sec);

        ret=gethostname(hostname,sizeof(hostname));
        if(ret==-1) {
                fprintf(stderr,"gethostname() error.\n");
                exit(-1);
        }

        memset(hname,0,128);
        strcpy(hname,hostname);
        for(p=strtok(hname,"-");p;p=strtok(NULL,"-")) {
                                h_name=p;
        }

//      printf("INTF_NUM=%d\n===================================\n",INTF_NUM);
        printf("%s\n",nowtime);
        printf("hostname=%s\n",hostname);
//      printf("hname=%s\n",str);

        for(i=0;i<INTF_NUM;i++) {
                memset(snd_buf,0,1024);

                sd=socket(PF_INET,SOCK_STREAM,0);
                if(sd==-1) {
                        perror("socket()");
                        exit(-1);
                }

                flags=fcntl(sd,F_GETFL);
                if(flags==-1) {
                        fprintf(stderr,"F_GETFL error.\n");
                        exit(-1);
                }
        //      printf("F_GETFL flags=%d\n",flags);

                flags|=O_NONBLOCK;
                ret=fcntl(sd,F_SETFL,flags);
                if(ret==-1) {
                        fprintf(stderr,"F_SETFL error.\n");
                        exit(-1);
                }
        //      printf("F_SETFL flags=%d\n",flags);
        //      printf("*******************\n");

        //      flags=fcntl(sd,F_GETFL);
        //      printf("sd=%d,ip=%s,port=%d,flags=%d\n",sd,ip[i],port[i],flags);

                his_end.sin_family=AF_INET;
                his_end.sin_port=htons(port[i]);
                his_end.sin_addr.s_addr=inet_addr(ip[i]);

                ret=connect(sd,(struct sockaddr *)&his_end,sizeof(his_end));
        //      printf("sd=%d,ret=%d,err=%s\n",sd,ret,strerror(errno));
                if(ret==0) {
                        fprintf(stderr,"send_to_169 connect() ok.\n");
                }
                else if(ret==-1 && errno==EINPROGRESS) {
                        fdmax=sd;
                //      fdmax=0;
                //      printf("sd=%d\n",sd);

                        FD_ZERO(&rfds);
                        FD_ZERO(&wfds);
                        FD_ZERO(&exfds);

                        FD_SET(sd,&rfds);
                        FD_SET(sd,&wfds);
                        FD_SET(sd,&exfds);

                        tv.tv_sec=2;
                        tv.tv_usec=0;

                        ret=select(fdmax+1,&rfds,&wfds,&exfds,&tv); 
                        printf("select ret=%d\n",ret);
                //      if(ret>0) {
                        if(ret==2 && FD_ISSET(sd,&rfds) && FD_ISSET(sd,&wfds)) {
                        //      fprintf(stderr,"select() ok, %s -> %s(%s):%s(%d) ok.\n",h_name,ip_name[i],ip[i],port_name[i],port[i]);
                                printf("Connection refused\n");
                                snprintf(snd_buf,1024,"select() Connection refused, %s -> %s(%s):%s(%d).\n",h_name,ip_name[i],ip[i],port_name[i],port[i]);
                                printf("%s",snd_buf);

                                ret=send_to_169(snd_buf);
                                if(ret==-1) {
                                        fprintf(stderr,"send_to_169() error.\n");
                                        exit(-1);
                                }
                        }
                        else if(ret==1 && FD_ISSET(sd,&wfds)) {
                                printf("select() ok, %s -> %s(%s):%s(%d) ok.\n",h_name,ip_name[i],ip[i],port_name[i],port[i]);
                        }
                        else if(ret==0){
                        //      fprintf(stderr,"select() timeout.\n");
                                printf("select() timeout.\n");
                                snprintf(snd_buf,1024,"select() timeout, %s -> %s(%s):%s(%d)\n",h_name,ip_name[i],ip[i],port_name[i],port[i]);
                                printf("snd_buf=%s",snd_buf);

                                ret=send_to_169(snd_buf);
                                if(ret==-1) {
                                        fprintf(stderr,"send_to_169() error.\n");
                                        exit(-1);
                                }
                        }
                        else {
                        //      fprintf(stderr,"select() error.\n");
                                printf("select() error.\n");
                                snprintf(snd_buf,1024,"select() error, %s -> %s(%s):%s(%d)\n",h_name,ip_name[i],ip[i],port_name[i],port[i]);
                                printf("snd_buf=%s",snd_buf);

                                ret=send_to_169(snd_buf);
                                if(ret==-1) {
                                        fprintf(stderr,"send_to_169() error.\n");
                                        exit(-1);
                                }
                        }
                }

                /*
                ret=shutdown(sd,SHUT_RD);
                printf("shutdown ret=%d\n",ret);
                perror("shutdown()");
                */

                ret=close(sd);
                if(ret==-1) {
                        fprintf(stderr,"close() error.\n");
                        exit(-1);
                }


                if(i!=4) {
                        printf("-----------------------------------------\n");
                        sleep(2);
                }
                else {
                        printf("-------------------------------------------------------------------------------\n\n");
                }
        }

        //      close(sd);

        exit(0);
}

你可能感兴趣的:(cms,Stream,struct,socket,null,SOAP)