First page Back Continue Last page Overview Graphics
Example of getnameinfo()
int s; /* socket */
ssize_t cc;
char buf[256];
struct sockaddr_storage ss;
socklen_t sslen = sizeof(ss);
char hbuf[NI_MAXHOST], serv[NI_MAXSERV];
sslen = sizeof(ss);
cc = recvfrom(s, buf, sizeof(buf),
(struct sockaddr *)&ss, &sslen);
if (cc >= 0 &&
getnameinfo((struct sockaddr *)&ss, sslen,
hbuf, sizeof(hbuf), pbuf, sizeof(pbuf),
NI_NUMERICHOST|NI_NUMERICSERV) == 0) {
printf(ā%d bytes from %s port %s\nā,
(int)cc, hbuf, pbuf);
}
Notes: