Groupsock (Port)--live555 source Read (iv) network

Source: Internet
Author: User
Tags htons

Groupsock (Port)--live555 source Read (iv) network
    • Groupsock (Port)--live555 source Read (iv) network
      • Brief introduction
      • Definition of the Port class
      • Port construction and global << operator overloading

This article by Mob Lym fabricated, welcome reprintblog.cnblogs.net/oloroso
This article by Mob Lym fabricated, welcome reprintmy.oschina.net/oloroso

Brief introduction

The port class is used to save the network port, the computer network port generally has two meanings, respectively is the physical sense network device interface and the logical sense port. This refers to the logical port (specifically the TCP/IP port in the protocol), the range of the port is 0 to 65535 (u_int16_t of the Representation range).

PortClass has only one data member to portNumBits fPortNum hold the port value. The saved is expressed in the network byte order. The network byte order is the big endian.
The byte order is slightly mentioned here. The byte order is divided into 大端序 and 小端序 . Take the carcass here, which fPortNum is 16bits width, which accounts for two bytes. If it represents the memory block 0x1000 and 0x1001 the two bytes, and the contents of the storage is 0x5678, then is that byte representing 0x56 that byte 0x78 ? This involves the problem of byte order. Usually the low address is saved and the high address is called the small end sequence. The reverse is the big-endian sequence.

Big
memory Address Small-order representation 0x5678endian indicates 0x5678
0x1001 0x56 0x78
0x1000 0x78 0x56
Definition of the Port class
1 typedef u_int16_t Portnumbits;2 classPort {3  Public:4Port (portnumbits num/*In host byte order*/);5 6Portnumbits num ()Const //In network byte order7     {8         returnFportnum;9     }Ten  One Private: APortnumbits Fportnum;//stored in network byte order - #ifdef IRIX -Portnumbits filler;//hack to overcome a bug in IRIX C + + compiler the #endif -};

The construction of port and the global <<Operator overloading

Construction is the assignment of the internal fPortNum , note that the time of assignment is converted to the network byte order. htonsThe function is host to network,return short meant to convert the parameters from the num host order to the network order (possibly with the same host sequence and network order). When all the arguments are passed, the biography is converted to the network sequence.

1 /*  */) {2     fportnum = htons (num); 3 }

Overloading the global << is to use BasicUsageEnvironmen the T object to output the port value. The function here ntohs is an htons inverse operation that converts the network byte order to the host byte order.

1 operator Const port& p) {2     return s << Ntohs (P.num ()); 3 }

Groupsock (Port)--live555 source Read (iv) network

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.