I am getting the following errors when i gcc my code:

44) unixs1 $ gcc whoisserver.c
whoisserver.c: In function `main':
whoisserver.c:59: warning: passing arg 2 of `bind' from incompatible pointer type
whoisserver.c:71: warning: passing arg 2 of `accept' from incompatible pointer type
Undefined                       first referenced
 symbol                             in file
bind                                /var/tmp//ccSx8OhK.o
getservbyname                       /var/tmp//ccSx8OhK.o
accept                              /var/tmp//ccSx8OhK.o
listen                              /var/tmp//ccSx8OhK.o
gethostbyname                       /var/tmp//ccSx8OhK.o
socket                              /var/tmp//ccSx8OhK.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

I know my libraries are correct and was looking for help resolving the undefined symbols.

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


#define BACKLOG          5          /* # of requests to queue              */
#define MAXHOSTNAME      32         /* maximum host name length            */

int main(argc,argv)
int argc;
char *argv[];
{
   int s, t;                        /* socket descriptor                   */
   int i;                           /* general purpose integer             */
   u_short portbase =0;
   struct sockaddr_in sa, isa;      /* internet socket structure           */
   struct hostent *hp;              /* host name structure                 */
   char *myname;                    /* pointer to the name of this program */
   struct servent *sp;              /* pointer to srevice entry            */
   char localhost[MAXHOSTNAME+1];   /* local host name as character string */

   myname = argv[0];
   portbase = 8000;
   /*
    * Look up the WHOIS service entry
    */
   if ((sp = getservbyname("whois","tcp")) == NULL)
   {
      fprintf(stderr,"%s: No whois service on this host.\n",myname);
      exit(1);
   }   

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


   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;


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

    if (bind(s, &sa, sizeof sa) < 0)
    {
       perror("bind");
       exit(1);
    }

    listen(s, BACKLOG);

   while (1)
   {
      i = sizeof sa;

      if ((t = accept(s,&isa,&i)) < 0)
      {
         perror("accept");
         exit(1);
      }
      whois(t);      /* perform the actual WHOIS service */
      close(t);
   }
}


int whois(int sock);
{
   struct passwd *p;
   char buf[BUFSIZ+1];
   int i;


   if ((i = read(sock,buf,BUFSIZ)) <= 0)
      return;
   buf[i] = '\0';

   if ((p = getpwnam(buf)) == NULL)
       strcpy(buf,"User not found\n");
   else

       sprintf(buf,"%s: %s\n",p->pw_name,p->pw_gecos);

   write(sock,buf,strlen(buf));
   return;
}

Thanks for the help.

You are on Solaris, right? Add -lsocket -lnsl to the command line. That will pick up the necessary libraries.

This article has been dead for over six months. Start a new discussion instead.