Xref: utzoo alt.sys.sun:4025 comp.unix.wizards:25779 Newsgroups: alt.sys.sun,comp.unix.wizards Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!maverick.ksu.ksu.edu!ux1.cso.uiuc.edu!bradley.bradley.edu!brad From: brad@bradley.bradley.edu (Bradley E. Smith) Subject: Maybe I am missing something....but why doesn't this work? Message-ID: <1991May29.145535.23801@bradley.bradley.edu> Keywords: TCP SOCKETS Organization: Bradley University Date: Wed, 29 May 91 14:55:35 GMT Lines: 97 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 #include #include #include #ifdef SYSV #include #include #else #include #include #endif #include #include 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@bradley.edu --- 309-677-2337