Socket proggraming LOTS of unsolved error

Hey guys, I'm trying to follow beej's guide to networking but I get lots of errors. I'm a this point of the tutorial :
http://beej.us/guide/bgnet/output/html/multipage/ipstructsdata.html#ipaddrs1

I installed cygwin in order to compile the code unmodified as he recommended here :
http://beej.us/guide/bgnet/output/html/multipage/intro.html#windows

Here's my code :
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
#include <iostream>

using namespace std;

int main()
{


    struct addrinfo {
    int              ai_flags;     // AI_PASSIVE, AI_CANONNAME, etc.
    int              ai_family;    // AF_INET, AF_INET6, AF_UNSPEC
    int              ai_socktype;  // SOCK_STREAM, SOCK_DGRAM
    int              ai_protocol;  // use 0 for "any"
    size_t           ai_addrlen;   // size of ai_addr in bytes
    struct sockaddr *ai_addr;      // struct sockaddr_in or _in6
    char            *ai_canonname; // full canonical hostname

    struct addrinfo *ai_next;      // linked list, next node
    };

    struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
    };

    // (IPv4 only--see struct sockaddr_in6 for IPv6)

    struct sockaddr_in {
        short int          sin_family;  // Address family, AF_INET
        unsigned short int sin_port;    // Port number
        struct in_addr     sin_addr;    // Internet address
        unsigned char      sin_zero[8]; // Same size as struct sockaddr
    };

    // (IPv4 only--see struct in6_addr for IPv6)

    // Internet address (a structure for historical reasons)
    struct in_addr; {
        uint32_t s_addr; // that's a 32-bit int (4 bytes)
    };

    // (IPv6 only--see struct sockaddr_in and struct in_addr for IPv4)

    struct sockaddr_in6 {
        uint16_t       sin6_family;   // address family, AF_INET6
        uint16_t       sin6_port;     // port number, Network Byte Order
        uint32_t       sin6_flowinfo; // IPv6 flow information
        struct in6_addr sin6_addr;     // IPv6 address;
        uint32_t       sin6_scope_id; // Scope ID
    };

    struct in6_addr {
        unsigned char   s6_addr[16];   // IPv6 address
    };

    struct sockaddr_storage {
    sa_family_t  ss_family;     // address family

    // all this is padding, implementation specific, ignore it:
    char      __ss_pad1[_SS_PAD1SIZE];
    int64_t   __ss_align;
    char      __ss_pad2[_SS_PAD2SIZE];
    };

    struct sockaddr_in sa; // IPv4
    struct sockaddr_in6 sa6; // IPv6

    inet_pton(AF_INET, "192.0.2.1", &(sa.sin_addr)); // IPv4
    inet_pton(AF_INET6, "2001:db8:63b3:1::3490", &(sa6.sin6_addr)); // IPv6

    // IPv4:

    char ip4[INET_ADDRSTRLEN];  // space to hold the IPv4 string
    struct sockaddr_in sa;      // pretend this is loaded with something

    inet_ntop(AF_INET, &(sa.sin_addr), ip4, INET_ADDRSTRLEN);

    printf("The IPv4 address is: %s\n", ip4);


    // IPv6:

    char ip6[INET6_ADDRSTRLEN]; // space to hold the IPv6 string
    struct sockaddr_in6 sa6;    // pretend this is loaded with something

    inet_ntop(AF_INET6, &(sa6.sin6_addr), ip6, INET6_ADDRSTRLEN);

    printf("The address is: %s\n", ip6);

    return 0;
}


I get these errors :

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
||=== Build: Debug in Beej's guide to network - 1 (compiler: GNU GCC Compiler) ===|
s guide to network - 1\main.cpp||In function 'int main()':|
s guide to network - 1\main.cpp|31|error: field 'sin_addr' has incomplete type|
s guide to network - 1\main.cpp|39|warning: unused variable 's_addr' [-Wunused-variable]|
s guide to network - 1\main.cpp|48|error: field 'sin6_addr' has incomplete type|
s guide to network - 1\main.cpp|57|error: 'sa_family_t' does not name a type|
s guide to network - 1\main.cpp|60|error: '_SS_PAD1SIZE' was not declared in this scope|
s guide to network - 1\main.cpp|62|error: '_SS_PAD2SIZE' was not declared in this scope|
s guide to network - 1\main.cpp|68|error: 'AF_INET' was not declared in this scope|
s guide to network - 1\main.cpp|68|error: 'struct main()::sockaddr_in' has no member named 'sin_addr'|
s guide to network - 1\main.cpp|68|error: 'inet_pton' was not declared in this scope|
s guide to network - 1\main.cpp|69|error: 'AF_INET6' was not declared in this scope|
s guide to network - 1\main.cpp|69|error: 'struct main()::sockaddr_in6' has no member named 'sin6_addr'|
s guide to network - 1\main.cpp|73|error: 'INET_ADDRSTRLEN' was not declared in this scope|
s guide to network - 1\main.cpp|74|error: redeclaration of 'main()::sockaddr_in sa'|
s guide to network - 1\main.cpp|65|error: 'main()::sockaddr_in sa' previously declared here|
s guide to network - 1\main.cpp|76|error: 'struct main()::sockaddr_in' has no member named 'sin_addr'|
s guide to network - 1\main.cpp|76|error: 'ip4' was not declared in this scope|
s guide to network - 1\main.cpp|76|error: 'inet_ntop' was not declared in this scope|
s guide to network - 1\main.cpp|78|error: 'printf' was not declared in this scope|
s guide to network - 1\main.cpp|83|error: 'INET6_ADDRSTRLEN' was not declared in this scope|
s guide to network - 1\main.cpp|84|error: redeclaration of 'main()::sockaddr_in6 sa6'|
s guide to network - 1\main.cpp|66|error: 'main()::sockaddr_in6 sa6' previously declared here|
s guide to network - 1\main.cpp|86|error: 'struct main()::sockaddr_in6' has no member named 'sin6_addr'|
s guide to network - 1\main.cpp|86|error: 'ip6' was not declared in this scope|
||=== Build failed: 22 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===| 


Sorry if I put all the errors in there raw.
If you have any idea on how to solve even one error, please tell me as it would help me a lot.
I suspect it has something to do with a lib to include or something like that but I'm not sure.
You're missing a bunch of includes I think.
Some are mentioned on this page:
http://www.linuxhowtos.org/C_C++/socket.htm

Hi,
I tried the includes on the page but it recognizes only the sys/ypes.h header.
For the others, it says 'no such file or directory'
Any ideas ?
try googling some of the errors. for example.
'inet_ntop' was not declared in this scope

it might give you clues as to which headers to include.
I tried googling a lot of the errors but nothing comes up.
post updated code please.
This is on linux yes?

edit:
there are also obvious syntax errors in there as well. for example:
1
2
3
    struct in_addr; {
        uint32_t s_addr; // that's a 32-bit int (4 bytes)
    };

can you see what you've done wrong there?

You've declared this twice:
 
struct sockaddr_in6 sa6; 


here's some headers you definitely need:

1
2
3
4
#include <iostream>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h> 
Last edited on
You don't say what operating system you're on nor what compiler you're using, although since you installed cygwin, I would presume you're on windows.

The code you posted includes many socket declarations (lines 9-76). These declarations are normally provided by the socket implementation you're using and it's unconventional to provide them yourself.

Here's a link to implementing a socket application under windows.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms737629(v=vs.85).aspx

Just looking at a couple of the errors in what you posted.

line 31 uses in_addr but in_addr is not declared until line 38. It must be declared before it is used.

Line 38. extraneous ;

Line 57: sa_family_t is not defined anywhere.

line 76: No function prototype for inet_pton.

My suggestion is to forget including your own socket declarations and use the ones provided by your os / compiler.

This is on linux yes?


The guy that is doing the tutorial I'm following is using linux but I'm using windows.
Here's the link to the tutorial : http://beej.us/guide/bgnet/output/html/multipage/index.html

post updated code please


I didn't change much in the code and accordingly to what @AbstractionAnon said, there seems to be a easier way to do what I'm looking to do.

You don't say what operating system you're on nor what compiler you're using


My bad, I'm using windows and codeblocks with the GNU GCC compiler.

The code you posted includes many socket declarations (lines 9-76). These declarations are normally provided by the socket implementation you're using and it's unconventional to provide them yourself.


I did not know that. AS you may have noticed, I'm a total noob when it comes to socket proggraming. I'm looking to make a simple IRC chat client in console. I asked where I could learn what I needed in order to do that here :
http://www.cplusplus.com/forum/beginner/166768/
They said to go check out beej's tutorial so that's what I did.

My suggestion is to forget including your own socket declarations and use the ones provided by your os / compiler.


Looks like I just complicated things. Where could I find a good tutorial for a simple implementation of the ones provided by my OS or compiler ? Is the msdn link a good guide to use or do you know a better link ?

Thanks,

mindoo
Any ideas ?
@koothkeeper
What do you mean ?
I already installed cygwin.
I mean go to their website and read the documentation. There is a ton of it there.
Ok, thanks but I started to follow the msdn tutorial above and its pretty good so I think I will stay with that tutorial.
I think you might have a Cygwin-specific issue: You should look at their FAQ and perhaps their mailing lists for questions about the socket implementation under Cygwin.
Topic archived. No new replies allowed.