#1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    undefined functions in g++


    - i get 2 errors for functions that are defined in
    sys/socket.h.

    Code:
    /**************************************************************************
    *       this is an object oriented client                                 *
    
    **************************************************************************/
    #include <stdarg.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <resolv.h>
    
    #define MAXBUF  1024
    #define PORT    9999
    
    struct message{
            int     ident;
            char    data[MAXBUF];};
    
    class cOneThrdClient{
            private:
                    int             sockfd;
                    int             bytes_read;
                    struct          sockaddr_in dest;
                    message         block;
                    char            serv[40];
            public:
                    cOneThrdClient(char *mess, char *server);
                    bool connekt();
                    bool sndMsg();
                    bool recvMsg();
    };
    
    cOneThrdClient::cOneThrdClient(char *mess, char *server){
            strcpy(block.data,mess); bytes_read = 0; block.ident = 69;
            if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
                    perror("Socket");}
    
            /*---Initialize server address/port struct---*/
            bzero(&dest, sizeof(dest));
            dest.sin_family = AF_INET;
            if ( inet_addr(server, &dest.sin_addr.s_addr) == 0 ){  <-- error in this function call
                    perror(serv);}
    
            dest.sin_port = htons(PORT);
    }
    
    bool cOneThrdClient::connekt(){
            if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) != 0 ){
                    perror("Connect");}
    }
    
    bool cOneThrdClient::sndMsg(){
            send(sockfd, &block, sizeof(block), 0);
    }
    
    bool cOneThrdClient::recvMsg(){
            /*---While there's data, read and print it---*/
            do{
                    bzero(&block, sizeof(block));
                    bytes_read = recv(sockfd, &block, sizeof(block), 0);
                    if ( bytes_read > 0 )
                            printf("%s\n", block.data);}
            while ( bytes_read > 0 );
    
            /*---Clean up---*/
            close(sockfd);   <-- error in this function call
    }
    
    int main(int Count, char *Strings[])
    {
            cOneThrdClient *cPtr = new cOneThrdClient(Strings[1], Strings[2]);
    
            cPtr->connekt(); cPtr->sndMsg(); cPtr->recvMsg();
    
            return 1;
    }
    errors:
    cClient.cpp: In constructor `cOneThrdClient::cOneThrdClient(char*, char*)':
    cClient.cpp:40: `inet_addr' undeclared (first use this function)
    cClient.cpp:40: (Each undeclared identifier is reported only once for each
    function it appears in.)
    cClient.cpp: In member function `bool cOneThrdClient::recvMsg()':
    cClient.cpp:65: `close' undeclared (first use this function)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    I don't know anything about sockets, but with file I/O close() is a function of the fstream class. Based on your error, maybe you have to call,

    close(sockfd)

    on an object.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222

    Re: undefined functions in g++


    Originally posted by infamous41md
    - i get 2 errors for functions that are defined in
    sys/socket.h.

    ...

    errors:
    cClient.cpp: In constructor `cOneThrdClient::cOneThrdClient(char*, char*)':
    cClient.cpp:40: `inet_addr' undeclared (first use this function)
    cClient.cpp:40: (Each undeclared identifier is reported only once for each
    function it appears in.)
    cClient.cpp: In member function `bool cOneThrdClient::recvMsg()':
    cClient.cpp:65: `close' undeclared (first use this function)
    I assume that this is for Linux.

    inet_addr() is declared in 3 header files (ie, I think you need all three):
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    close() is declared in unistd.h

    Required header files found on the functions' man pages.

    Yeah, it is a bit of a pain that UNIX/Linux has the sockets functions and data structures spread out over so many different header files. But then sockets is supposed to be able to support all kinds of protocol families, not just TCP/IP, so I guess they had to split it up.

    EDIT:
    BTW FWIW, on my Red Hat 7 I just grep'd for close and for inet_addr in sys/socket.h and did not find them. So sys/socket.h is apparently needed for things in netinet/in.h, which is needed for things in arpa/inet.h. inet_addr is declared in arpa/inet.h.
    Last edited by dwise1_aol; May 9th, 2003 at 09:44 AM.

IMN logo majestic logo threadwatch logo seochat tools logo