makala 发表于 2015-11-24 08:01:27

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, &quot;Usage: %s {i4|i6|<num>} string\n&quot;, argv);
exit(EXIT_FAILURE);
}
domain = (strcmp(argv, &quot;i4&quot;) == 0) ? AF_INET :
(strcmp(argv, &quot;i6&quot;) == 0) ? AF_INET6 : atoi(argv);
s = inet_pton(domain, argv, buf);
if (s <= 0) {
if (s == 0)
fprintf(stderr, &quot;Not in presentation format&quot;);
else
perror(&quot;inet_pton&quot;);
exit(EXIT_FAILURE);
}
if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
perror(&quot;inet_ntop&quot;);
exit(EXIT_FAILURE);
}
printf(&quot;%s\n&quot;, str);
exit(EXIT_SUCCESS);
}
页: [1]
查看完整版本: socket ntop pton两个函数的使用