Maybe I am missing something....but why doesn't this work?

Bradley E. Smith brad at bradley.bradley.edu
Thu May 30 00:55:35 AEST 1991


I am not sure why but the following program has problems.....

HARDWARE USED:

SUN 470 SunOS 4.1.1		- NO
SUN SLC SunOS 4.1.1		- NO
IBM RT BSD OS			- YES
AT&T 3B2 WIN TCP 3.2		- YES
AT&T 386 WIN TCP 3.0		- YES

What happens:  As the program is listed below (make sure you change
the host name if you are going to try it), 'bind(2)' fails on the
suns with EADDRNOTAVAIL.  The other machines work just fine.

If I change/remove the x(), and the '{' '}' so that the whole
program is in 1 function (ie I just delete a few lines everything
is in main()).  It works just fine.

Any thoughts?


#include	<stdio.h>
#include	<sys/types.h>
#include	<sys/socket.h>
#include	<errno.h>
#ifdef SYSV
#include	<sys/in.h>
#include	<sys/inet.h>
#else
#include	<netinet/in.h>
#include	<arpa/inet.h>
#endif
#include	<sys/param.h>
#include	<netdb.h>

extern int errno;

main()
{
	x();
}
x()
{
	int sock2, length;	
	struct sockaddr_in backupserver;
	struct hostent *hp;
	extern struct hostent *gethostbyname();
	extern char *inet_ntoa();
	int sockoptval;
	
	sockoptval = 1;

	hp = gethostbyname("bradley");
	if(hp == NULL) {
		sock2 = errno * -1;
		return(sock2);	/* unknown error */
	}
	sock2 = socket(AF_INET, SOCK_STREAM, 0);
	if(sock2 < 0) {
		sock2 = errno * (-1);
		return(sock2);
	}

	if( setsockopt(sock2, SOL_SOCKET, SO_DEBUG, &sockoptval,
	    sizeof(sockoptval)))
		perror("setsockopt");

	if( setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &sockoptval,
	    sizeof(sockoptval)))
		perror("setsockopt");

	backupserver.sin_family = AF_INET;

	bcopy((char *) hp->h_addr, (char *) &backupserver.sin_addr.s_addr,
		hp->h_length);

	backupserver.sin_port = htons(4321);
	length = sizeof(struct sockaddr_in);

	if(bind(sock2, (struct sockaddr *) &backupserver, length)) {
		perror("bind");
		sock2 = errno * (-1);
		return(sock2);
	}
	length = sizeof(backupserver);

	if(getsockname(sock2, (struct sockaddr *)&backupserver, &length)) {
		sock2 = errno * (-1);
		return(sock2);
	}
	(void) printf("OK: Socket has port # %d\n", ntohs(backupserver.sin_port));
	return(sock2);
}
-- 
Bradley Smith
Network & Technical Services @ Bradley University, Peoria, IL
brad at bradley.edu ---  309-677-2337



More information about the Comp.unix.wizards mailing list