1

Hell all. I was working on a server c project, using UDP for a whois service. But I got error: "No whois is service on this host." Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pwd.h>

#define BACKLOG        5           /* # of requests we're willing to queue */
#define MAXHOSTNAME    32          /* maximum host name length we tolerate */

main(argc,argv)
int argc;                      /* standard UNIX argument declarations  */
char *argv[];
{
int s,t;                       /* socket descriptors                   */
int i;                         /* general purpose integer              */
struct sockaddr_in sa,isa;     /* Internet socket address structure    */
struct hostent *hp;            /* result of host name lookup           */
char *myname;                  /* pointer to name of this program      */
struct servent *sp;            /* result of service lookup             */
char localhost[MAXHOSTNAME+1]; /* local host name as character string  */

myname = argv[0];
/*
 * Look up the WHOIS service entry
 */
if((sp = getservbyname("whois","udp")) == NULL){
    fprintf(stderr, "%s: No whois service on this host\n", myname);
    exit(1);
}
/*  
 * Get our own host information
 */
gethostname(localhost, MAXHOSTNAME);

if((hp = gethostbyname(localhost)) == NULL){
    fprintf(stderr, "%s: cannot get local host info?\n", myname);
    exit(1);
}

printf("host name is: %s\n",hp->h_name);
printf("my name is: %s\n",myname);

/*
 * Put the WHOIS socket number and our address info into the socket structure
 */
u_short portbase = 0;
portbase = 5000;
sa.sin_port = sp->s_port;
sa.sin_port = htons(ntohs((u_short)sp->s_port)+portbase);
bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
sa.sin_family = hp->h_addrtype;

/*
 * Allocate an open socket for incoming connections
 */
if((s = socket(hp->h_addrtype, SOCK_DGRAM, 0)) < 0){
    perror("socket");
    exit(1);
}



/*
 * Bind the socket to the service port
 */
if(bind(s, (struct sockaddr *)&sa, sizeof sa) < 0){
    perror("bind");
    exit(1);
}

/*
 * Set maximum connections we will fall behind
 */
//listen(s,BACKLOG);
/*
 * Go into an infinite loop waiting for new connections
 */
while(1){
    i = sizeof isa;
    /*
     * We hang in accept() while waiting for new customers
     */
    /*
    if((t = accept(s, (struct sockaddr *)&isa, &i)) < 0){
        perror("accept");
        exit(1);
    }
    */
    whois(s);                          /* perform the actual WHOIS service */
    close(s);
}   
}
/*
 * Get the WHOIS request from remote host and format a reply.
 */
whois(sock)
int sock;
{
struct sockaddr_in clientAddr;
socklen_t len = sizeof(clientAddr);
memset(&clientAddr, 0, sizeof(clientAddr));
struct passwd *p;
char buf[BUFSIZ+1];
int i;
/*
 * Get one line request
 */
printf("start to recv data\n");
if((i = recvfrom(sock, buf, BUFSIZ, 0, (struct sockaddr*)&clientAddr, &len)) <= 0)
printf("recv failed\n");    
return;
buf[i] = '\0';                          /* Null terminate */

printf("After the read, the buf is: %s \n",buf);

/*
 * Look up the requested user and format reply
 */
if((p = getpwnam(buf)) == NULL)
    strcpy(buf, "User not found\n");
else
    sprintf(buf, "%s: %s (from me)\n", p->pw_name, p->pw_gecos);
/*
 * Return reply
 */
//write(sock, buf, strlen(buf));    
sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&clientAddr, len);
return;
}

I couldn't figure out where's error. I have a similar code using TCP for whois which runs no problem.

4

1 に答える 1

3

WHOIS は TCP サービスです。UDP 経由では使用できません。

さらに、あなたが書いているのはWHOISサーバーではありません。WHOIS は、所有権情報を伝達するためにドメインおよび IP レジストラによって実装されるプロトコルです (たとえば、ドメイン名の所有者を検索するため)。あなたがここに書いているのは、ある種の NIS サービスのようです。これは WHOIS ではなく、同じポートを使用するべきではありません。

于 2013-10-21T15:14:02.973 に答える