Socket Programming Practice (2)--socket Programming Guide

Source: Internet
Author: User

What is a socket?

The socket can be seen as an interface between the user process and the Kernel network protocol stack (as shown in the programming interface), which can be used not only for native interprocess communication, but also for interprocess communication between different hosts on the network. It can even be used for communication between heterogeneous systems.

IPV4 Socket Interface Address structure

The IPV4 socket interface address structure is also commonly referred to as the " internetwork socket Address Structure ", which is named "Sockaddr_in" in the header file <netinet/in.h>

TCP/IP address structure struct sockaddr_in{    uint8_t  Sin_len;    sa_family_t  sin_family;    IN_PORT_TSIN_PORT;//2 byte    struct IN_ADDRSIN_ADDR;//4 byte    char sin_zero[8];//8 byte};

Member Description:

Sin_len: The length of the entire sockaddr_in struct, the first member before the 4.3bsd-reno version is sin_family.

sin_family: Specifies the address family, which must be set to Af_inet for IPV4

Sin_port: Port

The address of the Sin_addr:ipv4;

Sin_zero: Temporarily not used, it is generally set to 0

Linux Architecture (Common):

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

Generic address structure

Used to specify the address associated with the socket (other protocols can be supported).

struct sockaddr{uint8_t  sin_len;sa_family_t  Sin_family;char sa_data[14];//14 bytes};

Description

Sin_len: Length of the entire SOCKADDR structure

sin_family: Specify the Address family

Sa_data: Determined by the sin_family its form.

Network byte order

1. Big endian byte-order (Endian)

The most significant bit (msb:most significant bit) is stored at the lowest memory address , and the least significant bit (lsb:lowest significant bit) is stored at the highest memory address.

2. Small-endian byte order (Little Endian)

The most significant bit (msb:most significant bit) is stored at the highest memory address , and the least significant bit (lsb:lowest significant bit) is stored at the lowest memory address.

3. Host byte order

different hosts have different byte-order, such as x86 for small-endian, Motorola 6800 is the big end byte order, arm byte order is configurable.

4. Network byte order

network byte order defined as big endian byte order

Tests whether the current system is a small-ended mode int main () {    int data = 0x12345678;  int = 4 bytes (32 bits)                            //4 bits for 1-bit hexadecimal bit,//                            the 8-bit hexadecimal bit represents 4*8=32 bit bits    char *p = (char *) &data;    printf ("%x,%x,%x,%x\n", p[0],p[1],p[2],p[3]);    0x78 is low, if it is placed at P[0] (low address), then it is the small end mode if    (p[0] = = 0x78)    {        cout << "Current system is small end mode" << endl;// x86 platform is a small-end mode    }    else if (p[0] = = 0x12)    {        cout << "Current system is big-endian" << ENDL;//IBM is in endian mode    }}

BYTE-order conversion functions (commonly used for port conversions)

uint32_t htonl (uint32_t hostlong); uint16_t htons (uint16_t hostshort); uint32_t Ntohl (uint32_t netlong); uint16_t Ntohs ( uint16_t netshort)/** Description: H representative (local) Host;n Representative Network;s Representative Short;l representative long;*/
Test conversion result int main () {    int localedata = 0x12345678;    Char *p = (char *) &localeData;    printf ("Begin:%0x%0x%0x%0x\n", p[0], p[1], p[2], p[3]);    Convert local bytes to network byte    int inetdata = htonl (localedata);    p = (char *) &inetData;    printf ("After:%0x%0x%0x%0x\n", p[0], p[1], p[2], p[3]);    if (p[0] = = 0x12)        cout << "network system for big-endian mode" << Endl;    else        cout << "Network system for small-end mode" << Endl;    printf ("host:%x, inet:%x\n", LocaleData, Inetdata);}

Address Translation Functions ( for IP address translation )

#include <netinet/in.h> #include <arpa/inet.h>int inet_aton (const char *CP, struct in_addr *inp); in_addr_t INET_ADDR (const char *CP); char *inet_ntoa (struct in_addr in);
IN_ADDR is defined as follows: typedef uint32_t IN_ADDR_T;STRUCT in_addr{    in_addr_t s_addr;};


Practice int Main () {    //convert dotted decimal to decimal number    cout << inet_addr ("192.168.139.137") << Endl;    Convert decimal number into dotted decimal form    struct IN_ADDR address;    ADDRESS.S_ADDR = inet_addr ("192.168.139.137");    cout << Inet_ntoa (address) << Endl;    Memset (&address,0,sizeof (address));    Inet_aton ("127.0.0.1", &address);    cout << address.s_addr << Endl;    cout << Inet_ntoa (address) << Endl;    return 0;}

Socket type

1) Flow socket (SOCK_STREAM)

provide connection-oriented, reliable data transmission services, no errors, no duplication of transmission, and in the order of delivery received, corresponding to the TCP protocol.

2) Datagram-type sockets (SOCK_DGRAM)

provides no connection service. Do not provide error-free guarantees, data may be lost or duplicated, and receive order confusion, corresponding to the UDP protocol.

3) original socket (SOCK_RAW)

allows us to encapsulate the IP layer directly across the transport layer.

TCP client/server model


Simple echo Server model

Socket Programming Practice (2)--socket Programming Guide

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.