锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

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

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

套接字的结构和数据处理

In this section, I’ll cover various data types used by the sockets interface, since some of them are a real bear to figure out.

在本节中,我将介绍套接字接口使用的各种数据类型。因为他们中的一些需要理解。

First the easy one: a socket descriptor. A socket descriptor is the following type: 第一个最简单的一个:一个套接字描述符。一个套接字描述符以下类型:

Just a regular int. 只是一个普通int。

Things get weird from here, so just read through and bear with me. Know this: there are two byte orderings: most significant byte (sometimes called an "octet") first, or least significant byte first. The former is called "Network Byte Order". Some machines store their numbers internally in Network Byte Order, some don’t. When I say something has to be in Network Byte Order, you have to call a function (such as htons()) to change it from "Host Byte Order". If I don’t say "Network Byte Order", then you must leave the value in Host Byte Order.

事情从这里变得奇怪,所以只要通读和耐心等待。知道这一点:有两种字节顺序:最高有效字节(有时称为“八位字节”)优先,或最低有效字节优先。前者被称为“网络字节顺序”。有些机器内部数字存储在网络字节顺序,有些没有。当我说有些事情必须在网络字节顺序,你必须调用一个函数(比如htons())把它从主机字节顺序改变过来。如果我没有说“网络字节顺序”,那么你必须离开主机字节顺序的文本值。

(For the curious, "Network Byte Order" is also know as "Big-Endian Byte Order".) (求知:“网络字节顺序”也被称为“Big-Endian字节顺序”。)
My First StructTM–struct sockaddr. This structure holds socket address information for many types of sockets: 我的第一个StructTM-struct 套接字字体:该结构包含各种类型套接字地址信息:

struct sockaddr {
          unsigned short sa_family; // address family, AF_xxx
          char sa_data[14]; // 14 bytes of protocol address
          };

sa_family can be a variety of things, but it’ll be AF_INET for everything we do in this document. sa_data contains a destination address and port number for the socket. This is rather unwieldy since you don’t want to tediously pack the address in the sa_data by hand.

sa_family可以是各种各样的形式,但是在这个文档中,它会为任何事成为AF_INET。sa_data包含一个目的地套接字的地址和端口号。这是很不实用的,因为你不想手动挑选繁琐的sa_data地址。

To deal with struct sockaddr, programmers created a parallel structure: struct sockaddr_in ("in" for "Internet".) 为了处理套接字地址的结构,程序员创建了一个平行结构:struct sockaddr_in ("in" for "Internet".)。

struct sockaddr_in {
          short int sin_family; // Address family
          unsigned short int sin_port; // Port number
          struct in_addr sin_addr; // Internet address
          unsigned char sin_zero[8]; // Same size as struct sockaddr
          };

This structure makes it easy to reference elements of the socket address. Note that sin_zero (which is included to pad the structure to the length of a struct sockaddr) should be set to all zeros with the function memset(). Also, and this is the important bit, a pointer to a struct sockaddr_in can be cast to a pointer to a struct sockaddr and vice-versa. So even though socket() wants a struct sockaddr*, you can still use a struct sockaddr_in and cast it at the last minute! Also, notice that sin_family corresponds to sa_family in a struct sockaddr and should be set to "AF_INET". Finally, the sin_port and sin_addr must be in Network Byte Order!

这种结构可以很容易地引用套接字地址的元素。注意,sin_zero(包括垫的长度一个套接字地址的结构)memset()函数应该被设置为0 。同时,这也是最重要的一点,一个指向 SOCKADDR_IN的结构可以转换为一个指向套接字地址的结构,反之亦然。所以即使socket() 想要一个套接字地址的结构,你仍然可以使用一个sockaddr_in结构并且在最后一分钟计算它!同时,注意sin_family相当于套接字地址结构的sa_family 而且应该被设置为“AF_INET”。最后,sin_port和sin_addr必须在网络字节顺序!

"But," you object, "how can the entire structure, struct in_addr sin_addr, be in Network Byte Order?" This question requires careful examination of the structure struct in_addr, one of the worst unions alive: // Internet address (a structure for historical reasons)
你会问,“如何把整个struct in_addr sin_addr结构改变成网络字节顺序?“这问题需要仔细的检查struct in_addr的结构,最糟糕的情况是一个组合的存在:/ /网络地址(一个结构由于历史原因)

struct in_addr {
          unsigned long s_addr;
          };

Well, it used to be a union, but now those days seem to be gone. Good riddance. So if you have declared ina to be of type struct sockaddr_in, then ina.sin_addr.s_addr references the 4-byte IP address (in Network Byte Order). Note that even if your system still uses the God-awful union for struct in_addr, you can still reference the 4-byte IP address in exactly the same way as I did above (this due to #defines.)

它曾经是一个并集,但是现在那些日子似乎消失了。终于解脱了。如果你已经声明ina为sockaddr_in的一个结构类型,那么ina.sin_addr.s_addr引用4字节的IP地址(网络字节顺序)。请注意,即使你的系统仍然使用可憎的struct in_addr集合,你仍然可以参考4字节的IP地址以完全相同的方式引用。像我上面做的(这是因为#defines。)

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