home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / next / programm / 5810 < prev    next >
Encoding:
Text File  |  1992-08-26  |  2.6 KB  |  98 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!psinntp!relief!jjfeiler
  3. From: jjfeiler@relief.com (John Jay Feiler)
  4. Subject: "bind"ing sockets, -ObjC, and memset....A puzzle
  5. Message-ID: <1992Aug26.043241.2355@relief.com>
  6. Sender: jjfeiler@relief.com
  7. Reply-To: jjfeiler@relief.com
  8. Organization: relief consulting
  9. Date: Wed, 26 Aug 1992 04:32:41 GMT
  10. Lines: 86
  11.  
  12. I had a strange problem today, and I thought I'd throw it out here to see if  
  13. anyone had an explanation for it.  (at least one better than mine)
  14.  
  15. Background:
  16.  
  17. I'm doing some heterogenous ipc using internet sockets, so I wrote a small  
  18. program to test my socket implementation, basically copied out of one of the  
  19. Nutshell books.  Worked great.  Took the same code, and put in inside a  
  20. method of an ObjC object.  No dice.  The call to bind failed, regardless of  
  21. what port I gave it.  Hmmmm..., says I, what could be the problem?  Somehow,  
  22. I got it down to the compiler flag -ObjC making the differnece.  The  
  23. following (seriously incomplete) code, without the call to memset(&sin,...),  
  24. will fail to bind correctly if compiled with -ObjC, but works fine without  
  25. the -ObjC flag.
  26.  
  27. -------foo.m------------
  28. #import <sys/types.h>
  29. #import <sys/socket.h>
  30. #import <netinet/in.h>
  31. #import <netdb.h>
  32. #import <stdio.h>
  33. #import <libc.h>
  34. extern int errno;
  35.  
  36. int main()
  37. {
  38.     char hostname[64];
  39.     register int s;
  40.     struct hostent *hp;
  41.     struct sockaddr_in sin;
  42.     int    i;
  43.         
  44.     gethostname(hostname,sizeof(hostname));
  45.  
  46.     if((hp = gethostbyname(hostname)) == NULL)
  47.     {
  48.         fprintf(stderr,"%s: unknown host.\n",hostname);
  49.         exit (1);
  50.     }
  51.  
  52.     if((s = socket(AF_INET,SOCK_STREAM,0))<0)
  53.     {
  54.         perror("server: socket");
  55.         exit(1);
  56.     }
  57.     
  58.     /********************
  59.         if this memset is not performed, bind will likely fail
  60.         with errno 49.
  61.         could not bind to address.  Don't ask me why, It just
  62.         does......
  63.     ********************/
  64.     memset(&sin,0,sizeof(sin));
  65.     
  66.     
  67.     sin.sin_family = AF_INET;
  68.     bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
  69.     
  70.     /***********
  71.         bind the address to the socket
  72.  
  73.         let's try all the ports we can think of, until we find one
  74.  
  75.         we should get #1024, because 1-1023 are reserved
  76.     ***********/
  77.     
  78.     for(i=1;i<30000;i++)
  79.     {
  80.         sin.sin_port = htons(i);
  81.         if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0)
  82.             printf("bound to port %d\n",i);
  83.     }
  84.     close(s);
  85.     exit(0);
  86. }
  87. ---------------------------
  88.  
  89. Now, it seems to me that bind shouldn't be looking at anything else in the  
  90. sockaddr structure, other than the fields I'm setting, but somehow the memset  
  91. call solves the problem.  Any ideas as to what might be causing things to  
  92. fail without the memset and with the -ObjC flag?
  93.  
  94.     Puzzled,
  95.  
  96.         John
  97.  
  98.