From: dave@csd4.csd.uwm.edu (Dave Rasmussen)
Subject: Alpha/OSF network code question
Date: 30 May 1993 19:19:53 GMT [thread overview]
Message-ID: <1ub1cpINNgpb@uwm.edu> (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
next reply other threads:[~1993-05-30 19:19 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1993-05-30 19:19 Dave Rasmussen [this message]
1993-05-31 20:46 ` Alpha/OSF network code question Dave Rasmussen
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox