The tragedy of Htons and htonl

Source: Internet
Author: User
Tags htons

Do not say anything, first throw the question :

Client: Connect:connection refused

The following issues occur with the debug server:

Port: Incoming server port number, set to 8888

Srvaddr.sin_port: The local port number is converted to a network port number and stored in the network socket interface address data structure

This is 0!

HTONL is used here.

This is the client who died in htons, dividing the client and server at both ends of the galaxy.

cause : The data type of sin_port in the network socket address data structure is not noticed, and the htonl can be arbitrarily substituted for htons use.

IPV4 Universal Network Socket Interface address data structure:

struct sockaddr_in {                sa_family_t    sin_family; /* address family: af_inet  */               in_port_t       sin_port;   /* port in network byte  order */                struct in_addr sin_addr;   /* internet address */            };       /* internet  address. */           struct in_addr  {               uint32_t        s_addr;     /* address in network byte order */            };


Where:in_port_t : TCP or UDP port, typically uint16_t

Ps: data type in POSIX.LG

Data type

Description

Header file

int8_t

Signed 8-bit integer

Sys/types.h

uint8_t

Unsigned 8-bit integer

Sys/types.h

int16_t

Signed 16-bit integer

Sys/types.h

uint16_t

Unsigned 16-bit integer

Sys/types.h

int32_t

Signed 32-bit integer

Sys/types.h

uint32_t

Unsigned 32-bit integer

Sys/types.h

sa_family_t

Address clusters for the address structure of the socket interface

Sys/socket.h

Socklen_t

The length of the set interface address, typically uint32_t

Sys/socket.h

in_port_t

TCP or UDP port, typically uint16_t

Netinet/in.h

in_addr_t

Ipv4 address, typically uint32_t

Netinet/in.h

byte sort function

There are two kinds of data storage in computer memory, one is small- endian, that is, low-address storage data low-byte, high-address storage data high-byte, one is the big- endian byte-order , that is, the low address stores the high byte, the high address stores the low byte. The byte order of the network is big endian, and the host byte order may be the small-endian byte order, so there is the problem of byte-order conversion.

Function:

uint16_t htons (uint16_t hostvalue); uint32_t htonl (uint32_t hostvalue); uint16_t ntohs (uint16_t netvalue); uint16_t Ntohl (uint32_t netvalue);

header file:netinet/in.h

Description: the H representation in the function means that the host,n means that the network,s represents short,l for long, using the htons conversion port number, htonl and Ntohl to convert the IP address.

Results at a glance .....

Ps: Part of the content quoted from: http://blog.csdn.net/xiaoweibeibei/article/details/6584250


The tragedy of Htons and htonl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.