锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / C语言开源技术 / 套接字IP地址以及如何处理
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。

套接字IP地址以及如何处理

Fortunately for you, there are a bunch of functions that allow you to manipulate IP addresses. No need to figure them out by hand and stuff them in a long with the « operator.

幸运的是,有一堆函数允许您操作你的IP地址。不需要用手计算,也不需要移位操作符<<来填充生成。。

First, let’s say you have a struct sockaddr_in ina, and you have an IP address "10.12.110.57" that you want to store into it. The function you want to use, inet_addr(), converts an IP address in numbers-and-dots notation into an unsigned long. The assignment can be made as follows:
首先,假设你有一个sockaddr_in ina结构而且你有一个IP地址“10.12.110.57”想要存储进出。函数要使用,inet_addr(),将一个以点分隔的IP地址字符串转换成一个长整数。分配可以如下:

ina.sin_addr.s_addr = inet_addr("10.12.110.57");    

Notice that inet_addr() returns the address in Network Byte Order already–you don’t have to call htonl(). Swell! 注意,inet_addr返回地址输入到网络字节顺序,你不必调用htonl()!

Now, the above code snippet isn’t very robust because there is no error checking. See, inet_addr() returns -1 on error. Remember binary numbers? (unsigned)-1 just happens to correspond to the IP address 255.255.255.255! That’s the broadcast address! Wrongo. Remember to do your error checking properly.

现在,上面的代码片段不是很强大,因为没有错误检查。看,inet_addr()返回-1错误。还记得二进制数吗?(无符号)-1恰好对应的IP地址是255.255.255.255 !这是广播地址!记住正确的做你的错误检查。

Actually, there’s a cleaner interface you can use instead of inet_addr(): it’s called inet_aton() ("aton" means "ascii to network"): 实际上,有一个更简洁的界面可以代替inet_addr()使用:这叫做inet_aton()(“aton”的意思“网络ascii”):

#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

And here’s a sample usage, while packing a struct sockaddr_in (this example will make more sense to you when you get to the sections on bind() and connect().) 这是一个示例使用,因为包装到sockaddr_in结构(当你接触bind()和connect()这个例子会更有意义。)

struct sockaddr_in my_addr;
   my_addr.sin_family = AF_INET; // host byte order
   my_addr.sin_port = htons(MYPORT); // short, network byte order
   inet_aton("10.12.110.57", &(my_addr.sin_addr));
   memset(&(my_addr.sin_zero), ’\0’, 8); // zero the rest of the struct

inet_aton(), unlike practically every other socket-related function, returns non-zero on success, and zero on failure. (If someone knows why, please tell me.) And the address is passed back in inp. inet_aton(),有别于所有其他的套接字的相关函数,在成功时,返回非零,失败时返回零。(如果有人知道为什么,请告诉我。)地址在INP传回。

Unfortunately, not all platforms implement inet_aton() so, although its use is preferred, the older more common inet_addr() is used in this guide. 不幸的是,并不是所有平台都能实现inet_aton()所以,尽管它的使用是首选,老程序员对inet_addr()使用更加普遍。

All right, now you can convert string IP addresses to their binary representations. What about the other way around? What if you have a struct in_addr and you want to print it in numbers-and-dots notation? In this case, you’ll want to use the function inet_ntoa() ("ntoa" means "network to ascii") like this:

好了,现在你可以将字符串IP地址转换为二进制表示。反过来呢?如果你有一个in_addr结构,你想打印数字和点格式?在这种情况下,你会想要使用inet_ntoa()函数(“ntoa”意味着“网络ascii”),像这样:

printf("%s", inet_ntoa(ina.sin_addr));

That will print the IP address. Note that inet_ntoa() takes a struct in_addr as an argument, not a long. Also notice that it returns a pointer to a char. This points to a statically stored char array within inet_ntoa() so that each time you call inet_ntoa() it will overwrite the last IP address you asked for. For example:
这将打印IP地址。注意,inet_ntoa()取走一个 in_addr结构作为参数。还要注意,它返回一个指针为char。这指向一个在inet_ntoa()内的静态存储char数组,以便每次你调用inet_ntoa()时,它将覆盖最后一个你要的IP地址。例如:

char *a1, *a2;
    .
    .
   a1 = inet_ntoa(ina1.sin_addr); // this is 192.168.4.14
   a2 = inet_ntoa(ina2.sin_addr); // this is 10.12.110.57
   printf("address 1: %s\n",a1);
   printf("address 2: %s\n",a2);

will print: 将打印:

address 1: 10.12.110.57
   address 2: 10.12.110.57

If you need to save the address, strcpy() it to your own character array. 如果您需要保存地址,strcpy()到自己的字符数组。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内