C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 8th, 2003, 09:23 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
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)

Reply With Quote
  #2  
Old May 9th, 2003, 02:43 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old May 9th, 2003, 09:35 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,793 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 6 h 53 m 57 sec
Reputation Power: 434
Re: undefined functions in g++

Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > undefined functions in g++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway