htons function

The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian).

Syntax

u_short htons(
  u_short hostshort
);

Parameters

hostshort

A 16-bit number in host byte order.

Return Value

The htons function returns the value in TCP/IP network byte order.

Remarks

The htons function takes a 16-bit number in host byte order and returns a 16-bit number in network byte order used in TCP/IP networks (the AF_INET or AF_INET6 address family).

The htons function can be used to convert an IP port number in host byte order to the IP port number in network byte order.

The htonsfunction does not require that the Winsock DLL has previously been loaded with a successful call to the WSAStartup function.

Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.

Requirements

   
Minimum supported client Windows 8.1, Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header winsock.h (include Winsock2.h)
Library Ws2_32.lib
DLL Ws2_32.dll

See Also

InetNtop

InetPton

WSAHtonl

WSAHtons

WSANtohl

WSANtohs

htond

htonf

htonl

htonll

inet_addr

inet_ntoa

ntohd

ntohf

ntohl

ntohll

ntohs