socket ntop pton两个函数的使用
#include <arpa/inet.h>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
unsigned char buf;
int domain, s;
char str;
if (argc != 3) {
fprintf(stderr, "Usage: %s {i4|i6|<num>} string\n", argv);
exit(EXIT_FAILURE);
}
domain = (strcmp(argv, "i4") == 0) ? AF_INET :
(strcmp(argv, "i6") == 0) ? AF_INET6 : atoi(argv);
s = inet_pton(domain, argv, buf);
if (s <= 0) {
if (s == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}
if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
perror("inet_ntop");
exit(EXIT_FAILURE);
}
printf("%s\n", str);
exit(EXIT_SUCCESS);
}
页:
[1]