Sockets?

The program compiles fine, but when run it keeps displaying "Run Failed". Anyone know why? Thanks

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
/* Created September 1st
 Displays IP of host (IPv4/IPv6)*/


#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <iostream>
#include <string>
#include <arpa/inet.h>

using namespace std;

int main()
{
        addrinfo hints, *a, *socket;
        char conv [INET6_ADDRSTRLEN];
        memset (&hints,0,sizeof hints);
        hints.ai_family= AF_UNSPEC;
        hints.ai_family= SOCK_STREAM;
        if (getaddrinfo ("www.google.com", "4000",&hints, &socket)==1)
        {cout<<"Unable to recieve info";
        exit (1);}
        void* addr;
        string type;
        for (a=socket;a!=NULL;a=a->ai_next)
           
{
    if (a->ai_family=AF_INET)
    {
        sockaddr_in *IPv4= (struct sockaddr_in*) a->ai_addr;
        addr= &(IPv4->sin_addr);
        type= "IPv4";
    }  
    else
    {
        sockaddr_in6 *IPv6= (struct sockaddr_in6*)a->ai_addr;
        addr= &(IPv6->sin6_addr);
        type= "IPv6";
    }
}
        inet_ntop (a->ai_family, addr, conv, sizeof conv);
        cout<<type<<": "<<addr; 
        freeaddrinfo (socket);
        return 0;
}

I'm suprised line 18 compiled. It should be:

1
2
3

memset( &hints, 0, sizeof( hints ) );


Perhaps the way you have it sizeof hints is returning a smaller number and not clearing out the structure.

BTW you misspelled receive.
closed account (DSLq5Di1)
@kooth
http://stackoverflow.com/questions/1393582/why-is-sizeof-an-operator

@TheCreator
"Run Failed" isn't much to go on.. but after a bit of googling, it seems you are using NetBeans. A few links that may be of help:-

http://forums.netbeans.org/topic17858.html
http://stackoverflow.com/questions/3411386/program-wont-run-in-netbeans-but-runs-on-the-command-line
http://netbeans.org/community/releases/70/cpp-setup-instructions.html

If you still have problems I'd recommend posting on the NetBeans forums.
Thanks again, sloppy9! Again you have taught this old dog a new trick!
@sloppy9
Thanks for the links, but I don't think the problem is with the ide, because I tried running the code in Xcode but it still doesn't work. I checked through the code several times and the only mistake I could find was on line 29- I used '=' instead of '=='. I corrected that but still get run failed.

Thanks for all help.
closed account (DSLq5Di1)
Sorry, I didn't pay much attention to the source the first time I glanced over it,

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
int main()
{
        addrinfo hints, *a, *socket;
        char conv [INET6_ADDRSTRLEN];

        memset (&hints,0,sizeof hints);
        hints.ai_family= AF_UNSPEC;
        hints.ai_family= SOCK_STREAM; // should be ai_socktype

        if (getaddrinfo ("www.google.com", "4000",&hints, &socket)==1) // any non zero value indicates failure,
        {                                                              // change to != 0
            cout<<"Unable to recieve info";
            exit (1);
        }

        void* addr;
        string type;

        for (a=socket;a!=NULL;a=a->ai_next)
        {
            if (a->ai_family == AF_INET)
            {
                sockaddr_in *IPv4= (struct sockaddr_in*) a->ai_addr;
                addr= &(IPv4->sin_addr);
                type= "IPv4";
            }  
            else
            {
                sockaddr_in6 *IPv6= (struct sockaddr_in6*)a->ai_addr;
                addr= &(IPv6->sin6_addr);
                type= "IPv6";
            }
            cout << type << ": " << inet_ntop(a->ai_family, addr, conv, sizeof conv) << endl;
        }
        inet_ntop (a->ai_family, addr, conv, sizeof conv); // a == NULL here
        cout<<type<<": "<<addr;  // inet_ntop returns a char* to the string representation
        freeaddrinfo (socket);

        return 0;
}
Last edited on
Topic archived. No new replies allowed.