Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Alpha/OSF network code question
@ 1993-05-30 19:19 Dave Rasmussen
  1993-05-31 20:46 ` Dave Rasmussen
  0 siblings, 1 reply; 2+ messages in thread
From: Dave Rasmussen @ 1993-05-30 19:19 UTC (permalink / raw)



I've been looking at this code a while now and can't figure out what
conversion I need to do to the port I get from the getservbyname
in order to bind a name to the proper socket. Any advice to this or
anything else I'm doing would be appreciated.

I have the appropriate entry in the services file by the way.
Running netstat, I can't seem to get it to connect to the 
port I have defined in services.

/*
 * Some test server daemon code
 */
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
main (argc,argv) int argc; char **argv;	/* standard unix arguments */

{
struct  sockaddr_in  sa;	/*addresses for sockets*/  
struct  servent *sp;		/* service entry from /etc/services */ 
struct  hostent *hp;		/* host name entry */
char	hostnm[40];
int 	ss; /* for socket */		
unsigned short htons();

(void) gethostname(hostnm,40 );

if ((hp = gethostbyname(hostnm)) == NULL) {
	perror("gethostbyname");
	exit(1);
	}

sp = getservbyname("testsd", "tcp");
if (sp == NULL) {
	fprintf (stderr, "testsd: testsd/tcp unknown service\n");
	exit (-1);
}
	
/*
 * set up port number for socket address
 */
sa.sin_port =  htons(sp->s_port); /* what conversion should I use here??? */ 
sa.sin_family = hp->h_addrtype;

fprintf(stderr,"service=%s\n",sp->s_name);
fprintf(stderr,"htons(%d)=%d\n",sp->s_port,htons(sp->s_port));
printf("%d %d %d %d\n",hp->h_addr[0],hp->h_addr[1],hp->h_addr[2],hp->h_addr[3]);

bcopy((unsigned char *)hp->h_addr,(unsigned char *)&sa.sin_addr,hp->h_length);

if ((ss = socket(hp->h_addrtype,SOCK_STREAM,0)) < 0){
   perror("socket");
   exit(1);
   } 

printf("now to do the hinder binder\n");
/*
 * bind socket to port number
 */
if (bind (ss, &sa, sizeof sa) < 0 ){
	fprintf(stderr, "testsd: server socket bind failed - server down\n");
	perror("testsd_bind");
		exit (-1);
	}

getchar(); /* here just for me shove it in the background and see what's
	      going on */

}

-- 
Dave Rasmussen - SysAdm/Hacker/Consulting Manager, UWM Computing Svcs Div.
Internet:dave@uwm.edu, Uucp:uwm!dave, Bitnet:dave%uwm.edu@INTERBIT
AT&T:414-229-5133 USmail:Box 413 EMS380,Milwaukee,WI 53201  HAM: N9REJ       



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Alpha/OSF network code question
  1993-05-30 19:19 Alpha/OSF network code question Dave Rasmussen
@ 1993-05-31 20:46 ` Dave Rasmussen
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Rasmussen @ 1993-05-31 20:46 UTC (permalink / raw)


From article <1ub1cpINNgpb@uwm.edu>, by dave@csd4.csd.uwm.edu (Dave Rasmussen):
> 
> I've been looking at this code a while now and can't figure out what
> conversion I need to do to the port I get from the getservbyname
> in order to bind a name to the proper socket. Any advice to this or
> anything else I'm doing would be appreciated.
> 
> sa.sin_port =  htons(sp->s_port); /* what conversion should I use here??? */ 
> sa.sin_family = hp->h_addrtype;

Matt Thomas from DEC pointed out that I needed to bzero the socketaddr
structure of any remnant garbage before using it. Problem solved.

Thanks Matt!

-- 
Dave Rasmussen - SysAdm/Hacker/Consulting Manager, UWM Computing Svcs Div.
Internet:dave@uwm.edu, Uucp:uwm!dave, Bitnet:dave%uwm.edu@INTERBIT
AT&T:414-229-5133 USmail:Box 413 EMS380,Milwaukee,WI 53201  HAM: N9REJ       



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1993-05-31 20:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1993-05-30 19:19 Alpha/OSF network code question Dave Rasmussen
1993-05-31 20:46 ` Dave Rasmussen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox