Hey folks!
I am trying to compile showip.c in section 5.1 of Beej's Networking Tutorial.
And here are the compiler errors:Code:#include <stdio.h> #include <string.h> #include <winsock.h> #define INET6_ADDRSTRLEN 30 <=== I made 30 up to get rid of a compile error 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 }; // (IPv6 only--see struct sockaddr_in and struct in_addr for IPv4) struct sockaddr_in6 { u_int16_t sin6_family; // address family, AF_INET6 u_int16_t sin6_port; // port number, Network Byte Order u_int32_t sin6_flowinfo; // IPv6 flow information struct in6_addr sin6_addr; // IPv6 address u_int32_t sin6_scope_id; // Scope ID }; struct in6_addr { unsigned char s6_addr[16]; // IPv6 address }; int main(int argc, char *argv[]) { struct addrinfo hints, *res, *p; int status; char ipstr[INET6_ADDRSTRLEN]; WSADATA wsaData; // if this doesn't work //WSAData wsaData; // then try this instead // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0: if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); } if (argc != 2) { fprintf(stderr,"usage: showip hostname\n"); return 1; } memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version hints.ai_socktype = SOCK_STREAM; if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 2; } printf("IP addresses for %s:\n\n", argv[1]); for(p = res;p != NULL; p = p->ai_next) { void *addr; char *ipver; // get the pointer to the address itself, // different fields in IPv4 and IPv6: if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); ipver = "IPv4"; } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr); ipver = "IPv6"; } // convert the IP to a string and print it: inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); printf(" %s: %s\n", ipver, ipstr); } freeaddrinfo(res); // free the linked list return 0; }
I am using Win XP, I've added #include <winsock.h>, and I've added wsock32.lib to the project.------ Rebuild All started: Project: networking, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'networking', configuration 'Debug|Win32'
Compiling...
networking.c
c:\programs\networking\networking.c(20) : error C2016: C requires that a struct or union has at least one member
c:\programs\networking\networking.c(20) : error C2061: syntax error : identifier 'u_int16_t'
c:\programs\networking\networking.c(21) : error C2061: syntax error : identifier 'sin6_port'
c:\programs\networking\networking.c(21) : error C2059: syntax error : ';'
c:\programs\networking\networking.c(22) : error C2061: syntax error : identifier 'sin6_flowinfo'
c:\programs\networking\networking.c(22) : error C2059: syntax error : ';'
c:\programs\networking\networking.c(23) : error C2079: 'sin6_addr' uses undefined struct 'in6_addr'
c:\programs\networking\networking.c(24) : error C2061: syntax error : identifier 'sin6_scope_id'
c:\programs\networking\networking.c(24) : error C2059: syntax error : ';'
c:\programs\networking\networking.c(25) : error C2059: syntax error : '}'
c:\programs\networking\networking.c(55) : warning C4013: 'getaddrinfo' undefined; assuming extern returning int
c:\programs\networking\networking.c(56) : warning C4013: 'gai_strerror' undefined; assuming extern returning int
c:\programs\networking\networking.c(74) : error C2037: left of 'sin6_addr' specifies undefined struct/union 'sockaddr_in6'
c:\programs\networking\networking.c(79) : warning C4013: 'inet_ntop' undefined; assuming extern returning int
c:\programs\networking\networking.c(83) : warning C4013: 'freeaddrinfo' undefined; assuming extern returning int
Build log was saved at "file://c:\programs\networking\Debug\BuildLog.htm"
networking - 11 error(s), 4 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
I think there are more structures that need to be added to the program.
Has anyone managed to compile the beej programs successfully in Win XP, using VC++? I would really like to liase with you.
Thanks in advance.
EDIT: I can see line numbers alongside the code in VC++ 2008. Is there a way to copy the code and the line numbers, so I can paste it into this thread? The line numbers help people to see where the errors are.
EDIT #2: This program was written in linux, and here were the headers, which I removed and replaced with winsock.h, as per instructions:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>