Does `recv` work with bytes or octets, or are they one and the same in the context of POSIX documentation?

半腔热情 提交于 2019-12-24 03:09:38

问题


Reading the POSIX reference for socket send at http://pubs.opengroup.org/onlinepubs/009695399/functions/send.html I wonder, what exactly do they mean by "byte" - its traditional/historical meaning, its implied/popular meaning, or something else entirely? I am developing a binary TCP/IP-supported network protocol client-server program, and I would like to properly declare my buffer that I use to pass to recv, and being able to properly unpack its data.

Yes, I know it's almost hypothetical issue and probably a chance in a million to hit real problems, but better safe than sorry I guess.

So, do the POSIX guys mean octets really, or are they really implying that for platforms where a byte is say, 7 bits, send will deal with arrays with granularity of 7*N? Or maybe it will simply transfer all data bit by bit (size granularity (7 * n_bytes) bits)? I mean, since it expects a void * buffer...

Which declaration for my recv buffer would be most stable across architectures and compilers?

I am using GCC with the following flags (C99, POSIX):

cc -g -Wall -Wextra -std=c99 -pedantic -fexec-charset=ISO-8859-1 -DDEBUG -D_POSIX_C_SOURCE

回答1:


POSIX defines a byte as being the size of an octet, and defines an octet as 8 contiguous bits. This definitions are in chapter 3 of the POSIX standard, the latest POSIX standard (2008) is at http://pubs.opengroup.org/onlinepubs/9699919799/




回答2:


I believe that byte is the char type of C, the smallest possible small integer holding a characater, as supported by the system. (perhaps some strange compiler + target system do have non 8 bits char).

octet means a byte of 8 bits.

If you think in term of TCP/IP, I believe (IIRC) that it defines the various protocols in terms of 8 bit bytes. TCP is made of transmission of multiple of 8 bit bytes.

But in reality, you should not care that much. Perhaps documenting your prococol in terms of bits could help. A lot of things can make your protocol obsolete (just think of IPv6). If you protocol is really valuable and useful, and well documented, someone will make it live and adapt it.



来源:https://stackoverflow.com/questions/8107133/does-recv-work-with-bytes-or-octets-or-are-they-one-and-the-same-in-the-conte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!