What's the "hints" mean for the addrinfo name in socket programming
Asked Answered
F

4

17

When doing socket programming, people always name the addrinfo struct like this:

struct addrinfo hints;
// get ready to connect
status = getaddrinfo("www.example.net", "3490", &hints, &servinfo);

I'd like to know what it stands for, to better understand the struct.

Thanks in advance.


Thanks for the answers. Maybe I wasn't clear:

I want to know whether the variable name "hints" is some abbreviation for some words? Or if the word "hints" means it only gives some address info and let the getaddrinfo() function fill in the rest?

Finnougric answered 16/4, 2009 at 9:6 Comment(0)
F
14

From the FreeBSD man page:

hints is an optional pointer to a struct addrinfo, as defined by <netdb.h> ... This structure can be used to provide hints concerning the type of socket that the caller supports or wishes to use.

It's called "hints" because it can be used to provide, well, hints (in the sense of a tip; a suggestion that might come in useful but could be ignored). This indicates things like what protocol family (IPv4 vs. IPv6, for example) the caller wants, what socket type (datagram vs. straming), what protocol (TCP vs. UDP), etc. You can pass NULL in for hints and thus indicate that you don't care what protocol family, socket type, or protocol you get back.

Forgetmenot answered 17/4, 2009 at 2:39 Comment(0)
R
2

From http://linux.die.net/man/3/getaddrinfo

The hints parameter specifies the preferred socket type, or protocol. A NULL hints specifies that any network address or protocol is acceptable. If this parameter is not NULL it points to an addrinfo structure whose ai_family, ai_socktype, and ai_protocol members specify the preferred socket type. AF_UNSPEC in ai_family specifies any protocol family (either IPv4 or IPv6, for example). 0 in ai_socktype or ai_protocol specifies that any socket type or protocol is acceptable as well. The ai_flags member specifies additional options, defined below. Multiple flags are specified by logically OR-ing them together. All the other members in the hints parameter must contain either 0, or a null pointer.

Reo answered 16/4, 2009 at 9:13 Comment(1)
So from this, it follows that the example code is incorrect as shown; it fails to actually initialize the hints.ai_family field.Novah
S
0

struct addrinfo hints; is just a variable declaration. The struct itself is defined in a library and included via sys/socket.h.

Starfish answered 16/4, 2009 at 9:12 Comment(0)
E
-4

It refers to the same meaning which the English word "hints" mean. And it is not an abbreviation.

Embrocation answered 12/12, 2013 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.