Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie: socket api

Reply
Thread Tools

newbie: socket api

 
 
goran.pusic@gmail.com
Guest
Posts: n/a
 
      12-31-2012
On Wednesday, December 26, 2012 11:58:08 PM UTC+1, Miquel van Smoorenburg wrote:
> int r = getaddrinfo(ip, SERV_PORT, &hints, &res);
> if (r != 0) {
> fprintf(stderr, "%s/%s: %s\n", ip, SERV_PORT,
> gai_strerror(r));
> return NULL;
> }
> int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
> if (sock < 0)
> return -1;

A nitpick. Memory leak here: freeaddrinfo isn't called.

A get/freeaddrinfo wrapper, or use of ON_BLOCK_EXIT of ScopeGuard fame are on order.

Goran.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-01-2013
On Mon, 2012-12-31, Bill Cunningham wrote:
> Ian Collins wrote:
>> Thomas Kaufmann wrote:

>
> I would just like to say this one thing about this code. I've worked with
> the same functions in C with the unix socket api. I *think* bzero is
> depricated for memset now.


It is, but more importantly memset is standard C and C++; bzero never
was. And even more importantly, as stated two times upthread, all of
this messing about with memory and casts can be replaced with the
safer, more elegant and more powerful getaddrinfo().

>>> bzero(&servaddr, sizeof(servaddr) );
>>> servaddr.sin6_family = AF_INET;

....

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-04-2013
Jorgen Grahn wrote:
> It is, but more importantly memset is standard C and C++; bzero never
> was. And even more importantly, as stated two times upthread, all of
> this messing about with memory and casts can be replaced with the
> safer, more elegant and more powerful getaddrinfo().
>
>>>> bzero(&servaddr, sizeof(servaddr) );
>>>> servaddr.sin6_family = AF_INET;


Oh yes. getaddrinfo packs the structs for you. It's a little newer
addition to the API though. Some people may not know of it. I have an old
UNIX networking book that doesn't mention it. And a newer one that shows
how.

Bill


 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-05-2013
On Fri, 2013-01-04, Bill Cunningham wrote:
> Jorgen Grahn wrote:
>> It is, but more importantly memset is standard C and C++; bzero never
>> was. And even more importantly, as stated two times upthread, all of
>> this messing about with memory and casts can be replaced with the
>> safer, more elegant and more powerful getaddrinfo().
>>
>>>>> bzero(&servaddr, sizeof(servaddr) );
>>>>> servaddr.sin6_family = AF_INET;

>
> Oh yes. getaddrinfo packs the structs for you. It's a little newer
> addition to the API though. Some people may not know of it.


Yes, that was my point, and the reason I have mentioned it in three or four
postings now even though it's offtopic here.

> I have an old
> UNIX networking book that doesn't mention it. And a newer one that shows
> how.


Which one is this older one, by the way?

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
Re: Finding IP address of localhost via socket API (or other API) Timothy Grant Python 5 08-06-2008 03:35 AM



Advertisments