Binary Order function in Unix Network programming

Source: Internet
Author: User

Byteorder:

1#include <stdio.h>2 3 intMainintargcChar**argv)4 {5 Union {6          Shorts;7         Charc[sizeof( Short)];8 } un;9UN.S =0x0102;Ten      One     if(sizeof( Short) ==2 ) A     { -         if((un.c[0] ==1) && (un.c[1] ==2) ) -         { theprintf"big-endian\n"); -         } -         Else if((un.c[0] ==2) && (un.c[1] ==1) ) -         { +printf"little-endian\n"); -         } +         Else A         { atprintf"Unknown byte order.\n"); -         } -     } -     Else -     { -printf"unsupport.\n"); in     } -}
binaryorder.cc

The network protocol must have a network byte order , with a 16-bit port number and a 32-bit IPV4 address in each TCP subsection. The Send protocol stack and the accept protocol stack must agree on the order in which the bytes of these multibyte fields are routed.

the Internet protocol uses big-endian byte-order to transmit these multibyte integers .

Host byte order---> Network byte order:

#include <netinet/in.h>

uint16_t htons (uint16_t host16bitvalue);

uint32_t htonl (uint32_t host32bitvalue);

uint16_t Ntohs (uint16_t net16bitvalue);

uint32_t Ntohl (uint32_t net32bitvalue);

BYTE manipulation functions:

#include <string.h>

void *memset (void *dest, int c, size_t len);

void *memcpy (void *dest, const void *SRC, size_t nbytes);

int memcmp (const void *PTR1, const void *PTR2, size_t nbytes);

the ASCII string---the binary value of the > Network byte order (the value stored in the socket address structure) to convert the internetwork address.

#include <arpa/inet.h>

int Inet_aton (const char *strptr, struct in_addr *addrptr);

/** returns: 1 if the string is valid, otherwise 0**/

in_addr_t inet_addr (const char* STRPTR);

/** returns: The IPV4 address of the 32-bit binary network byte order if the string is valid, otherwise inaddr_none

Char *inet_ntoa (struct in_addr inaddr);

/** return: Pointer to a dotted decimal number string **/

int Inet_pton (int family, const char *strptr, void *addrptr);

/** If the success is 1, if the input is not a valid expression format is 0, if the error is -1**/

const char *inet_ntop (int family, const void *addrptr, char *strptr, size_t len);

/** returns: Jochen is a pointer to the result, or null**/if an error occurs

1 struct sockaddr_in addr; 2 sizeof (str)); 3 4 struct sockaddr_in6 ADDR6; 5 sizeof (str));

Binary Order function in Unix Network programming

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.