Cygwin socket problem

Thread Solved

Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Cygwin socket problem

 
0
  #1
Jul 19th, 2009
Hi, I installed cygwin recently and wanted to do some network programming with it. Since I have never used unix sockets before, I'm reading Beej's Guide To Network Programming. I took this from section 5.1 and tried to compile it. I get a few errors that I can't figure out .

  1. /*
  2. ** showip.c -- show IP addresses for a host given on the command line
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netdb.h>
  10. #include <arpa/inet.h>
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. struct addrinfo hints, *res, *p;
  15. int status;
  16. char ipstr[INET6_ADDRSTRLEN];
  17.  
  18. if (argc != 2) {
  19. fprintf(stderr,"usage: showip hostname\n");
  20. return 1;
  21. }
  22.  
  23. memset(&hints, 0, sizeof hints);
  24. hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
  25. hints.ai_socktype = SOCK_STREAM;
  26.  
  27. if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
  28. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
  29. return 2;
  30. }
  31.  
  32. printf("IP addresses for %s:\n\n", argv[1]);
  33.  
  34. for(p = res;p != NULL; p = p->ai_next) {
  35. void *addr;
  36. char *ipver;
  37.  
  38. // get the pointer to the address itself,
  39. // different fields in IPv4 and IPv6:
  40. if (p->ai_family == AF_INET) { // IPv4
  41. struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
  42. addr = &(ipv4->sin_addr);
  43. ipver = "IPv4";
  44. } else { // IPv6
  45. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
  46. addr = &(ipv6->sin6_addr);
  47. ipver = "IPv6";
  48. }
  49.  
  50. // convert the IP to a string and print it:
  51. inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
  52. printf(" %s: %s\n", ipver, ipstr);
  53. }
  54.  
  55. freeaddrinfo(res); // free the linked list
  56.  
  57. return 0;
  58. }

Here are my errors:
stuff.c: In function `main':
stuff.c:14: error: storage size of 'hints' isn't known
stuff.c:16: error: `INET6_ADDRSTRLEN' undeclared (first use in this function)
stuff.c:16: error: (Each undeclared identifier is reported only once
stuff.c:16: error: for each function it appears in.)
stuff.c:34: error: dereferencing pointer to incomplete type
stuff.c:40: error: dereferencing pointer to incomplete type
stuff.c:41: error: dereferencing pointer to incomplete type
stuff.c:45: error: dereferencing pointer to incomplete type
stuff.c:46: error: dereferencing pointer to incomplete type
stuff.c:51: error: dereferencing pointer to incomplete type

I added this to my code and got rid of the `INET6_ADDRSTRLEN' undeclared error:
  1. #ifndef INET6_ADDRSTRLEN
  2. #define INET6_ADDRSTRLEN 46
  3. #endif
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Cygwin socket problem

 
0
  #2
Jul 19th, 2009
I figured it out after about a bit of searching. For anyone having this same problem: apparently, Cygwin can't use IPv6 by default. I went to http://win6.jp/Cygwin/ and downloaded the files I needed there. I unzipped it into my cygwin directory, and had to replace cygwin1.dll in the bin directory with new-cygwin1.dll.
Last edited by TheBeast32; Jul 19th, 2009 at 11:49 pm.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: developer_borja is an unknown quantity at this point 
Solved Threads: 0
developer_borja developer_borja is offline Offline
Newbie Poster

Missing header include

 
0
  #3
2 Days Ago
I tried to put together the same program in C on our development server but ran into the same issue. I don't believe it's platform-related as much as it is leaving out a header include.

In reference to these two lines of Beej's showip.c example:
  1. addr = &(ipv4->sin_addr);
  2. addr = &(ipv6->sin6_addr);
If you're getting this error:
  1. error: dereferencing pointer to incomplete type
Chances are you left out:
  1. #include <netinet/in.h>
Cheers!

Matt Borja
Borja Webs, LLC
<<snipped>>
Last edited by niek_e; 2 Days Ago at 11:39 am. Reason: Fake sig removed
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC