🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.programmer
Article #802

"bind"ing sockets, -ObjC, and memset....A puzzle

#802
From: John Jay Feiler
Date: Wed, 26 Aug 1992 04:32
86 lines
2750 bytes
I had a strange problem today, and I thought I'd throw it out here to see if  
anyone had an explanation for it.  (at least one better than mine)

Background:

I'm doing some heterogenous ipc using internet sockets, so I wrote a small  
program to test my socket implementation, basically copied out of one of the  
Nutshell books.  Worked great.  Took the same code, and put in inside a  
method of an ObjC object.  No dice.  The call to bind failed, regardless of  
what port I gave it.  Hmmmm..., says I, what could be the problem?  Somehow,  
I got it down to the compiler flag -ObjC making the differnece.  The  
following (seriously incomplete) code, without the call to memset(&sin,...),  
will fail to bind correctly if compiled with -ObjC, but works fine without  
the -ObjC flag.

-------foo.m------------
#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <netdb.h>
#import <stdio.h>
#import <libc.h>
extern int errno;

int main()
{
	char hostname[64];
	register int s;
	struct hostent *hp;
	struct sockaddr_in sin;
	int	i;
		
	gethostname(hostname,sizeof(hostname));

	if((hp = gethostbyname(hostname)) == NULL)
	{
		fprintf(stderr,"%s: unknown host.\n",hostname);
		exit (1);
	}

	if((s = socket(AF_INET,SOCK_STREAM,0))<0)
	{
		perror("server: socket");
		exit(1);
	}
	
	/********************
		if this memset is not performed, bind will likely fail
		with errno 49.
		could not bind to address.  Don't ask me why, It just
		does......
	********************/
	memset(&sin,0,sizeof(sin));
	
	
	sin.sin_family = AF_INET;
	bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
	
	/***********
		bind the address to the socket

		let's try all the ports we can think of, until we find one

		we should get #1024, because 1-1023 are reserved
	***********/
	
	for(i=1;i<30000;i++)
	{
		sin.sin_port = htons(i);
		if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0)
			printf("bound to port %d\n",i);
	}
	close(s);
	exit(0);
}
---------------------------

Now, it seems to me that bind shouldn't be looking at anything else in the  
sockaddr structure, other than the fields I'm setting, but somehow the memset  
call solves the problem.  Any ideas as to what might be causing things to  
fail without the memset and with the -ObjC flag?

	Puzzled,

		John
 
Technical Details
Message-ID: <1992Aug26.043241.2355@relief.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!psinntp!relief!jjfeiler

Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.next.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!psinntp!relief!jjfeiler
From: jjfeiler@relief.com (John Jay Feiler)
Subject: "bind"ing sockets, -ObjC, and memset....A puzzle
Message-ID: <1992Aug26.043241.2355@relief.com>
Sender: jjfeiler@relief.com
Reply-To: jjfeiler@relief.com
Organization: relief consulting
Date: Wed, 26 Aug 1992 04:32:41 GMT
Lines: 86