变量由于某种原因正在改变

时间:2014-03-06 03:30:30

标签: c++ sockets

这篇文章是跟进的 Call to the 'userConnect()' method skips

对我上一篇文章的更正,它实际上是在输入userConnect()方法,但有些是如何更改SETUP_ERROR标志。正如您在我的userConnect()中看到的,我更改了SETUP_ERROR的位置,我清楚地显示了错误可能是什么。

现在使用'cout<<'在'getMyIPAddress()'方法中,SETUP_ERROR为0并进入userConnect()中的IF循环并且工作正常。当我删除它时,以某种方式将SETUP_ERROR更改为1并打印“SETUP_ERROR”。

无法弄清楚为什么SETUP_ERROR被改为1,我知道'cout'与此无关但它可能是什么。甘拜下风。

getMyIPAdress():

void getMyIPAddress (char* command, char* port) {
    struct hostent *he;
    struct in_addr ipv4addr;
    char dnsIP[] = "8.8.8.8";                                   // Google DNS IP
    char dnsPort[] = "53" ;                                     // Google DNS TCP Port
    // Converting IP to struct in_addr type     
    inet_pton(AF_INET, dnsIP, &ipv4addr);
    // getting host details from IP address
    he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
    cout << "bazinga!" << endl;
    // Connecting to google DNS to get IP address of this process
    userConnect (he->h_name, dnsPort, port, command);
}

userConnect():

void userConnect ( char* sIP, char* sPort, string my_port, char* command )
{
    int numbytes;
    char buf[MAXDATASIZE];
    char rbuf[MAXDATASIZE];
    int rv;
    char s[INET6_ADDRSTRLEN];
    struct addrinfo *servinfo,*p,hints;
    int sockfd;
    int SETUP_ERROR;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    // Getting address related information 
    // flagging invalid ip addresses 
    if ((rv = getaddrinfo(sIP, sPort, &hints, &servinfo)) != 0) {
        cout << "getaddrinfo: " << endl << gai_strerror(rv) << endl;
        cout << " Invalid IP Address! " << endl;
        SETUP_ERROR = 1;
    }else
    {
        int count;
        for(p = servinfo; p != NULL; p = p->ai_next) {
            if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
                perror("client: socket");
                continue;
            }

            if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
                close(sockfd);
                perror("client: connect");
                continue;
            }

            break;
        }
        // flagging invalid port numbers
        if (p == NULL) {
            cout << stderr << "client: failed to connect" << endl;
            cout  << "Invalid Port Number! " << endl;
            SETUP_ERROR = 1;
        }
    }
    if (SETUP_ERROR != 1) {

        inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);       // gettin printable ip
        freeaddrinfo(servinfo);                                                                 // don't need anymore
        int len;
        // Functionalities based on input commands
        if (strcmp(command,"REGISTER")==0) {
            // client adding server as default first connection
            conn_head = insert(conn_head , server_IP, sPort, sockfd-OFFSET );
            servfd = sockfd;
            len = my_port.length();
            // sending client's listening port number to server 
            if (send (sockfd, &len, sizeof( len ), 0) == -1)
                perror("send packet length");
            if (send(sockfd, my_port.c_str(), len, 0) == -1)
                perror("send");
            // adding socket fd to master list 
            addFdToMaster(sockfd);
        }else if(strcmp(command, "MYIP")==0) {
            /* Next 10 line code snippet is taken from a blog online! Cant find it anymore to post reference ! :) */
            //get local socket info::
            struct sockaddr_in local_addr;
                socklen_t addr_len = sizeof(local_addr);
            if (getsockname(sockfd, (struct sockaddr*)&local_addr, &addr_len) < 0) {
                perror("getsockname");
            }
            /* get peer ip addr */
            char my_ip[INET_ADDRSTRLEN];
            if (inet_ntop(local_addr.sin_family, &(local_addr.sin_addr), myip, sizeof(myip)) == NULL) {
                perror("inet_ntop");
            }
            else
                cout << "HOST IP Address:: " << myip << endl;
        }
    }else
        cout << "SETUP_ERROR" << endl;
}

1 个答案:

答案 0 :(得分:2)

您永远不会初始化SETUP_ERROR,也不会将其设置为0。测试if (SETUP_ERROR != 1)正在测试一个未初始化的变量,在您似乎期望它为0的情况下。

为了解释你所看到的内容,你的编译器将SETUP_ERROR放在堆栈上,并且因为没有初始化,所以它只假设来自内存位置的任何字节的值堆栈成长为。这些可能会受到您在其他功能中所做的影响。