943,867 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2959
  • C RSS
Jul 19th, 2009
0

Cygwin socket problem

Expand Post »
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
Similar Threads
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Jul 19th, 2009
0

Re: Cygwin socket problem

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.
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Nov 24th, 2009
0

Missing header include

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 Nick Evan; Nov 24th, 2009 at 11:39 am. Reason: Fake sig removed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
developer_borja is offline Offline
1 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with two programmes
Next Thread in C Forum Timeline: heap sort -- segmentation fault





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC