Socket network programming first, socket first
First, I would like to summarize some of the books required for job interviews: I suddenly felt very steadfast in my mind, "the beauty of programming", "The sword refers to offer", "Unix environment programming", "Unix network programming", and "POSIX multi-thread programming, I am no longer confused. I finally know what I can do after I graduated from the computer science field. Now I want to study hard and get an internship offer from bat. Come on.
Network byte order:
Because different computer systems store data in different byte order and a 4-byte 32-bit integer, the memory storage method is different. the byte order is divided into the Little Endian and the Big Endian. Most Intel processors use the Little Endian, and most Motorola processors use the Big Endian;
Each layer of TCP/IP protocol defines the byte order as the end. Therefore, the byte order used in TCP/IP protocol is generally called the network byte order.
1. convert an IP address to a hexadecimal number.
/*************************************** **********************************
> File Name: my_atoh.c
> Author: Comst
> Mail: 750145240@qq.com
> Created Time: Sat 07 Feb 2015 04:56:54 PM CST
**************************************** ********************************/
# Include <stdio. h>
# Include <string. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <arpa/inet. h>
# Define IP "180.97.33.107" // B461216B
Int main (int argc, char * argv [])
{
Struct in_addr addr;
Memset (& addr, 0, sizeof (addr ));
Inet_aton (IP, & addr );
Memset (& addr, 0, sizeof (addr ));
Addr. s_addr = inet_addr (IP );
Char * pstr = inet_ntoa (addr );
Return 0;
}
2. view the domain name details
/*************************************** **********************************
> File Name: my_host.c
> Author: Comst
> Mail: 750145240@qq.com
> Created Time: Sat 07 Feb 2015 05:23:46 PM CST
**************************************** ********************************/
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <netdb. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <arpa/inet. h>
Int main (int argc, char * argv [])
{
Struct hostent * pent;
Int I;
Pent = gethostbyname (argv [1]);
If (pent = NULL)
{
Perror ("host ");
}
Printf ("h_name: % s \ n", pent-> h_name );
For (I = 0; pent-> h_aliases [I]! = NULL; I ++)
{
Printf ("aliase % d: % s \ n", I, pent-> h_aliases [I]);
}
Printf ("addr_type: % d, len: % d \ n", pent-> h_addrtype, pent-> h_length );
For (I = 0; pent-> h_addr_list [I]! = NULL; I ++)
{
Printf ("ip % d: % s \ n", I, inet_ntoa (* (struct in_addr *) pent-> h_addr_list [I]);
}
Return 0;
}