The Origin and Function Definition of the byte Conversion Function htonl...

Source: Internet
Author: User

Byte conversion character origin:

There are many types of machines on the network. These machines are different in the byte order of data. For example, the i386 chip is low in the memory address,

The intel processor divides 32-bit integers into four consecutive bytes and stores them in byte order 1-2-3-4 in memory. 1 indicates the highest byte;

The alpha chip is the opposite. It is 4-3-2-1. If the integer memory is only copied one byte, two computers with different processors obtain different integer values,

For unification, the byte Conversion Function emerged;

 

The function is defined as follows:

Unsigned long int htonl (unsigned long int hostlong)

 

Unsigned short int htons (unisgned short int hostshort)

 

Unsigned long int ntohl (unsigned long int netlong)

 

Unsigned short int ntohs (unsigned short int netshort)

1. "htonl" is the abbreviation of "host to network long", meaning: the conversion of long integers from the host's byte order to the network's byte order;

2. In the four conversion functions, H represents host, N represents network. s represents short l represents long;

3. htonl (inaddr_any) indicates that the connection to any network interface of the server is allowed;

Attachment I: We do not recommend filling in this mode as follows:

Server. C code:

 

 1 /*  Make the necessary includes and set up the variables.  */ 2  3 #include <sys/types.h> 4 #include <sys/socket.h> 5 #include <stdio.h> 6 #include <netinet/in.h> 7 #include <arpa/inet.h> 8 #include <unistd.h> 9 #include <stdlib.h>10 11 int main()12 {13     int server_sockfd, client_sockfd;14     int server_len, client_len;15     struct sockaddr_in server_address;16     struct sockaddr_in client_address;17 18 /*  Create an unnamed socket for the server.  */19 20     server_sockfd = socket(AF_INET, SOCK_STREAM, 0);21 22 /*  Name the socket.  */23 24     server_address.sin_family = AF_INET;25     server_address.sin_addr.s_addr = inet_addr("127.0.0.1");26     server_address.sin_port = 40000;27     server_len = sizeof(server_address);28     bind(server_sockfd, (struct sockaddr *)&server_address, server_len);29 30 /*  Create a connection queue and wait for clients.  */31 32     listen(server_sockfd, 5);33     while(1) {34         char ch;35 36         printf("server waiting\n");37 38 /*  Accept a connection.  */39 40         client_len = sizeof(client_address);41         client_sockfd = accept(server_sockfd, 42             (struct sockaddr *)&client_address, &client_len);43 44 /*  We can now read/write to client on client_sockfd.  */45 46         read(client_sockfd, &ch, 1);47         ch++;48         write(client_sockfd, &ch, 1);49         close(client_sockfd);50     }51 }
Server. c

Client. C code:

 1 /*  Make the necessary includes and set up the variables.  */ 2  3 #include <sys/types.h> 4 #include <sys/socket.h> 5 #include <stdio.h> 6 #include <netinet/in.h> 7 #include <arpa/inet.h> 8 #include <unistd.h> 9 #include <stdlib.h>10 11 int main()12 {13     int sockfd;14     int len;15     struct sockaddr_in address;16     int result;17     char ch = ‘A‘;18 19 /*  Create a socket for the client.  */20 21     sockfd = socket(AF_INET, SOCK_STREAM, 0);22 23 /*  Name the socket, as agreed with the server.  */24 25     address.sin_family = AF_INET;26     address.sin_addr.s_addr = inet_addr("127.0.0.1");27     address.sin_port = 40000;28     len = sizeof(address);29 30 /*  Now connect our socket to the server‘s socket.  */31 32     result = connect(sockfd, (struct sockaddr *)&address, len);33 34     if(result == -1) {35         perror("oops: client2");36         exit(1);37     }38 39 /*  We can now read/write via sockfd.  */40 41     write(sockfd, &ch, 1);42     read(sockfd, &ch, 1);43     printf("char from server = %c\n", ch);44     close(sockfd);45     exit(0);46 }
View code


Running and problems:

 

 

 

 

 

 

The Origin and Function Definition of the byte Conversion Function 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.