Skip to content

Commit d7d426c

Browse files
author
Your Name
committed
getaddrinfo example
1 parent bb9e56b commit d7d426c

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

getaddrinfo.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdio.h>
2+
#include <netdb.h>
3+
#include <arpa/inet.h>
4+
5+
6+
int main(int argc, char **argv)
7+
{
8+
if (argc < 2) {
9+
fprintf(stderr, "need argument\n");
10+
return 1;
11+
}
12+
struct addrinfo hints, *result, *tmp;
13+
struct sockaddr_in *sock_addr;
14+
int res;
15+
hints.ai_flags = AI_PASSIVE;
16+
hints.ai_family = AF_INET;
17+
hints.ai_socktype = SOCK_STREAM;
18+
hints.ai_protocol = 0;
19+
hints.ai_addrlen = 0;
20+
hints.ai_canonname = NULL;
21+
hints.ai_addr = NULL;
22+
hints.ai_next = NULL;
23+
//hints.ai_protocol = IPPROTO_ICMP;
24+
25+
if ((res = getaddrinfo(argv[1], NULL, &hints, &result)) != 0) {
26+
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
27+
return 1;
28+
}
29+
30+
for (tmp = result; tmp; tmp = tmp->ai_next) {
31+
/*
32+
sockfd = socket(tmp->ai_family, tmp->ai.socktype, tmp->
33+
ai_protocol);
34+
if (sockfd == -1)
35+
continue;
36+
*/
37+
sock_addr = (struct sockaddr_in *)tmp->ai_addr;
38+
printf("address is: %s\n", inet_ntoa(sock_addr->sin_addr));
39+
}
40+
freeaddrinfo(result);
41+
return 0;
42+
}

sockets/RAW/ping.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* ping.c
33
* written by Ivan Ryabtsov ivriabtsov at gmail dot com
4-
* compile with: gcc -Wall -Wextra -g -O2 -DDEBUG=2 ping.c for maximum debug
5-
* gcc -Wall -Wextra -g -O2 -DDEBUG=1 ping.c for reduce debug
6-
* gcc -Wall -Wextra -g -O2 ping.c without debug
4+
* compile with: gcc -Wall -Wextra -pthread -g -O2 -DDEBUG=2 maximum debug
5+
* gcc -Wall -Wextra -pthread -g -O2 -DDEBUG=1 reduce debug
6+
* gcc -Wall -Wextra -pthread -g -O2 -DDEBUG=0 without debug
77
*/
88
#include <unistd.h>
99
#include <stdio.h>

0 commit comments

Comments
 (0)