C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-09-2007, 08:45 PM   #1
Registered User
 
Join Date: May 2007
Location: Berkeley, CA
Posts: 140
Why create a wrapper fo getaddrinfo()

Why do something like

Code:
struct addrinto *
host_serv(const char *host, const char *serv, int family, int socktype)
{
     int n;
     struct addrinfo hints, *res;
     bzero(&hints, sizeof(struct addrinfo));
     hints.ai_flags = AI_CANONNAME;
     hints.ai_famility = family;
     hints.ai_socktype = socktype;

     if( n =  getaddrinfo(host, serv, &hints, &res))  !=0)
          return (NULL);
     
    return (res);
}
And then call the function in main() like
Code:
struct addrinfo *ai;

ai = Host_serv(host, NULL, 0, 0);
Why not just call getaddrinfo() directly in main() like
Code:
struct addrinfo hints, *res;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags = AI_CANONNAME;
hints.ai_famility = family;
hints.ai_socktype = socktype;

if( n =  getaddrinfo(host, serv, &hints, &res) !=0)
     return (NULL);

Last edited by Overworked_PhD; 11-09-2007 at 08:50 PM.
Overworked_PhD is offline   Reply With Quote
Old 11-09-2007, 09:04 PM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
If I ask myself that question, the answer would be: convenience.
I'd rather call an easy function with a few params and get the data I require than fill out a troublesome struct and pass along.
It also makes the code cleaner and more structurized, if you ask me.
Elysia is offline   Reply With Quote
Old 11-10-2007, 01:37 AM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,689
> Why not just call getaddrinfo() directly in main() like
Why write functions at all?

The point would be to keep main from becoming a massive bloated function which is impossible to maintain.

Even if it has only a dozen lines, and you only ever call it once from just one place, it's still an improvement.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Create new combo boxes based on items selected in preview combo box RealityFusion Windows Programming 2 01-10-2007 09:50 AM
Can't create child windows OnionKnight Windows Programming 2 11-30-2006 08:11 PM
How to create a DLL wrapper. MindWorX C Programming 12 11-09-2006 02:52 PM
Need help to compile a GTK+ code with C++ sql wrapper kingsz1 C++ Programming 2 10-12-2006 06:17 PM
Create a file from c++ program Dan17 C++ Programming 2 05-08-2006 04:25 PM


All times are GMT -6. The time now is 02:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22