Hi, all.
I'm a relative novice when it comes to network programming, and I am
attempting to program a client/server app in C. The problem I am having is
when the server calls getpeername to find out the hostname of the client.
Every time I call getpeername, it returns a "bad file number" error. Here is
how I am calling it:
-----------------------------------------------------------------------
struct sockaddr peername;
int namelen;
struct hostent *host, *gethostbyaddr();
/* find out who is connected to us */
namelen = sizeof(peername);
if(getpeername(stdin, &peername, &namelen) == -1) {
/* error getting peername */
fprintf(logfile,"error [getpeername: %s]", strerror(errno));
}
else { /* Got peername */
fprintf(logfile,"[getpeername: %s]", peername.sa_data);
host = gethostbyaddr(peername.sa_data,sizeof(peername.sa_data), AF_INET);
if(host == NULL) {
fprintf(logfile,"error [gethostbyaddr: %s]", strerror(errno));
}
else {
fprintf(logfile,"[gethostbyaddr: %s]", host->h_name);
}
}
--------------------------------------------------------------------------
It is my understanding that, since I am using inetd to spawn this process,
the input and output sockets are provided to me in the form of standard
input and standard output. Thus, I use stdin as the socket parameter in the
call to getpeername.
Does anybody know what I am doing wrong? Any help would be appreciated.
Thanks,
---------------------------------------------------------------------------
TACTech, Inc. Voice: (714) 974-7676 ext.15
Yorba Linda, CA