htonl

Is network byte order pointless under IPv6?

拥有回忆 提交于 2019-12-10 10:45:34
问题 If we use a 32-bit integer to store an IPv4 address, then the byte order of the integer must be considered. However, as there is no built-in 128-bit integer type under almost all platforms, an IPv6 address must be stored into a byte array, so, I think the byte order is no longer a problem. Am I correct? Or is there a corresponding function htonlXXX for IPv6? 回答1: IPv6 does require network byte order for ipv6 addresses. hton and ntoh are all about converting the address from how you have it

ntohs, ntohl, htons,htonl的比较

≯℡__Kan透↙ 提交于 2019-12-07 20:44:28
最近的工作是进行程序由 SUN SPARC 向 Intel X86 移植的工作,多数问题都出现在网络字节序的部分,所以拿出来在这部分工作中使用频率较高的几个接口区分比较下:) ntohs =net to host short int 16位 htons=host to net short int 16位 ntohl =net to host long int 32位 htonl=host to net long int 32位 ntohs 简述: 将一个无符号短整形数从网络字节顺序转换为主机字节顺序。 #include u_short PASCAL FAR ntohs( u_short netshort); netshort:一个以网络字节顺序表达的16位数。 注释: 本函数将一个16位数由网络字节顺序转换为主机字节顺序。 返回值:ntohs()返回一个以主机字节顺序表达的数。 htons 简述: 将主机的无符号短整形数转换成网络字节顺序。 #include u_short PASCAL FAR htons( u_short hostshort); hostshort:主机字节顺序表达的16位数。 注释: 本函数将一个16位数从主机字节顺序转换成网络字节顺序。 返回值: htons()返回一个网络字节顺序的值。 ===================================

How to convert __u32 to __be32 in Linux Kernel

血红的双手。 提交于 2019-12-07 13:35:24
问题 I've a variable __be32 x; I've a function __u32 foo(void){ __u32 a; return a; } I need to store the return of foo in variable x . x=htonl(foo()); Is it correct? I've a confusion that what are the return types of ntohl() and htonl() . Are they opposite of each other? To check the output, I need to recompile the kernel and I don't want to trouble my system with any errors. So I'm asking here. 回答1: You can use the macros defined in kernel.h: http://www.bruceblinn.com/linuxinfo/ByteOrder.html The

When and how to use C++ htonl function

。_饼干妹妹 提交于 2019-12-06 03:07:07
问题 cout << "Hello World !" << endl; For my very first post on stack overflow : When are we supposed to use the htonl function? I have gone through the man page. However, I don't really understand when and how to use it. 回答1: H ost TO N etwork translation. It makes sure the endian of a 32 bit data value is correct (Big endian) for network transport. ntohl -- N etwork TO H ost -- is used by the receiver to ensure that the endian is correct for the receiver's CPU. Keep an eye out for htons and

How to convert __u32 to __be32 in Linux Kernel

你离开我真会死。 提交于 2019-12-05 18:55:43
I've a variable __be32 x; I've a function __u32 foo(void){ __u32 a; return a; } I need to store the return of foo in variable x . x=htonl(foo()); Is it correct? I've a confusion that what are the return types of ntohl() and htonl() . Are they opposite of each other? To check the output, I need to recompile the kernel and I don't want to trouble my system with any errors. So I'm asking here. You can use the macros defined in kernel.h: http://www.bruceblinn.com/linuxinfo/ByteOrder.html The following macros return the value after it has been converted. Note: the linux/kernel.h header file is the

When and how to use C++ htonl function

本小妞迷上赌 提交于 2019-12-04 08:34:31
cout << "Hello World !" << endl; For my very first post on stack overflow : When are we supposed to use the htonl function? I have gone through the man page. However, I don't really understand when and how to use it. H ost TO N etwork translation. It makes sure the endian of a 32 bit data value is correct (Big endian) for network transport. ntohl -- N etwork TO H ost -- is used by the receiver to ensure that the endian is correct for the receiver's CPU. Keep an eye out for htons and ntohs for handling 16 bits, and out there somewhere are likely htonll and ntohll for 64 bits. Using all of them

Portable C binary serialization primitives

本小妞迷上赌 提交于 2019-12-03 12:11:13
As far as I know, the C library provides no help in serializing numeric values into a non-text byte stream. Correct me if I'm wrong. The most standard tool in use is htonl et al from POSIX. These functions have shortcomings: There is no 64-bit support. There is no floating-point support. There are no versions for signed types. When deserializing, the unsigned-to-signed conversion relies on signed integral overflow which is UB. Their names do not state the size of the datatype. They depend on 8-bit bytes and the presence of exact-size uint_ N _t. The input types are the same as the output types

host to network double?

妖精的绣舞 提交于 2019-12-01 03:27:50
问题 I'd like to send some double precision floating point numbers over the network. (standard C, standard sockets) There is no htond or ntohd to convert the data to and from network byte order. What should I do? I have a couple solutions in my head but I'd like to know what the common practice is. (I'd also like to know what is the common practice for sending 64bit ints, like gint64 values used by gstreamer) edit: This is one solution I thought of. I presume it works for any size integers, but is

How to set sockaddr_in6::sin6_addr byte order to network byte order?

…衆ロ難τιáo~ 提交于 2019-11-30 09:03:16
I developing a network application and using socket APIs. I want to set sin6_addr byte order of sockaddr_in6 structure. For 16 bits or 32 bits variables, it is simple: Using htons or htonl: // IPv4 sockaddr_in addr; addr.sin_port = htons(123); addr.sin_addr.s_addr = htonl(123456); But for 128 bits variables, I dont know how to set byte order to network byte order: // IPv6 sockaddr_in6 addr; addr.sin6_port = htons(123); addr.sin6_addr.s6_addr = ??? // 16 bytes with network byte order but how to set? Some answers may be using htons for 8 times (2 * 8 = 16 bytes), or using htonl for 4 times (4 *

Confusion in htons- little endian/ big endian

╄→尐↘猪︶ㄣ 提交于 2019-11-29 13:42:43
问题 When I send a integer variable from one process to other through socket, and then printing the value at received end, the value is still the same without using ntohl/htonl, then where do I need to use these functions other than initializing socket structures. I understand litte/big endian. But why do we need to convert port and IP nos to host/network byte order when value remains the same. Please explain in detail how the integer is tranferred over network? 回答1: If you want your program to be