UNIX Network Programming Learning notes __ Programming

Source: Internet
Author: User
Tags ack terminates port number
StatementThis note does not involve SCTP, IPV6, and UNIX platform-specific knowledge. Easy to learn Winsock readers to read.
Chapter I. INTRODUCTIONDescription: Please read this blog about computer network notes Http://t.cn/zjQjulJ, here no longer repeat. Chapter II Transport layer: TCP and UDP User Packet Protocol UDP
The problem with UDP for network programming is the lack of reliability, which we also call UDP provides connectionless (connectionless) services, because UDP clients do not have to have a long-term relationship with the server.
TCP for Transmission Control Protocol
TCP provides a connection between the client and the server. A TCP client establishes a connection to a given server and exchanges data with the corresponding server across the connection, and then terminates the connection; TCP provides reliability. When TCP sends data to the other end, it requires an acknowledgement to be returned to the end. TCP automatically retransmissions data and waits longer if confirmation is not received. TCP does not give up after several retransmission failures. UDP provides unreliable datagram delivery. UDP itself does not provide acknowledgement, serial number, RTT estimation, timeout and retransmission mechanisms; TCP provides traffic control. TCP always tells the End-to-end how many bytes of data it can receive, which is called the notification window; The TCP connection is Full-duplex, which means that the application process can send or receive data at any time on a given connection. time_wait State
(1), to achieve the termination of TCP Full-duplex link reliability, that is, to ensure the reliable disconnection of TCP connection is explained: must handle the connection termination sequence of the four sections of any of the missing cases (for example, the active shutdown one end sent Ack lost, you may receive the fin again, and resend ack)
(2), let the old repeating section disappear in the network
Explanation: Suppose there is a connection on a pair of IP and ports, once the connection is closed, a new connection is established, and the latter is the previous incarnation, because the IP and port are the same, TCP must prevent the old group from a connection from reproducing after the connection terminates, thereby affecting the data interaction of the new connection. So TCP cannot start a new avatar for a connection in a time_wait state, which is misinterpreted as belonging to the same connection.
As for the time requirement of 2MSL (Maximum section Life period), TCP stipulates that the maximum lifetime of a section in the network is MSL, which is enough to allow a section in a direction to survive up to a maximum of MSL seconds to be discarded, and the response of the other direction to the maximum surviving MSL seconds is discarded, by this rule, You can guarantee that when a TCP connection is successfully established, the old repeating group of all the connections from the link before it disappears on the network.
The following illustration shows client hosts and servers on different LANs connected over a wide area network:
The following illustration shows that the concurrent server lets the child process handle the customer:
The second customer service is connected to the same server as shown in the following illustration:
TCP Output
Each TCP socket has a send buffer, and we can use the SO_SNDBUF socket option to change the size of the buffer. When an application process calls the Write function, the The kernel copies all data from the application process's buffer to the sending buffer of the write socket. If the socket's send buffer does not fit all the data for the process, the application will be put into sleep if the socket is blocked. The kernel will not return from the write system call, All data in the buffer of the application process is copied to the socket send buffer. Therefore, a successful return from a write call that writes a TCP socket simply means that we can reuse the original application process buffer and does not indicate TCP or application to the end. The following illustration shows the steps to apply a process to write a TCP socket and the buffer UDP output
This time we'll show the socket send buffer with the dotted box, because it doesn't actually exist. Any UDP socket has a send buffer size, but it is only the upper limit of the size of a UDP datagram that can be written to that socket. If an application writes a datagram that is larger than the socket buffer size, The kernel will return an error. Since UDP is unreliable, it does not have to save a copy of the application process data. So there's no need for a real send buffer. A write call that writes a UDP socket successfully returns an output queue that represents a datagram or that all of its fragments have been added to the data link layer. If the queue does not have enough space to hold the datagram or a fragment of it, The kernel returns an error to the application process. The steps and buffers involved in applying process write UDP sockets are shown in the following figure
Port number well-known ports: 0~1023 they are bound to some services that typically determine a protocol such as HTTP protocol port 80; Registered ports: 1024~49151 They loosely bind some services and can be dynamically distributed; Dynamic and private ports: 49152~65535 typically do not bind these ports for the service, which is used for dynamic allocation.
chapter III views on basic socket programming Socket Address Structure
The IPV4 socket address structure is often also referred to as an "internetwork socket address structure." The IPV4 Poxis is defined as follows.
[CPP]   View plain copy #include <<netinet/in.h>   typedef uint32_t in_addr_t;    struct in_addr   {    in_addr_t s_addr;             //uint32_t s_addr 32 bit IPV4 network byte order   };   typedef unsigned short int sa_family_t;   #define  __sockaddr_common (Sa_ Prefix)  sa_family_t sa_prefix# #family the use of # in   //c language    struct sockaddr    {    __SOCKADDR_COMMON  (Sa_);      //is Unsigned short  int sin_family;  Address Family     char sa_data[14];             //-specific protocol address   };   struct sockaddr_in    {    uint8_t sin_len;              //the knotThe length of the structure     __SOCKADDR_COMMON  (sin_);     //namely Unsigned short int  sin_family; protocol family  ipv4 for af_inet    in_port_t  sin_port;          //Port number     struct     in_addr sin_addr ;  //32 bit IPV4 network byte-order address     unsigned char sin_zero[sizeof  (STRUCT&NBSP;SOCKADDR)  - __SOCKADDR_COMMON_SIZE - sizeof  (in_port_t)  - sizeof  (struct  in_addr)]; //padding align bit   };  //Generally the above structure can be materialized as follows:   struct sockaddr_in    {    uint8_t        sin_len;       //signed 8-bit integer address structure length     sa_family_t    sin_family;    //protocol family, IPv4 for af_inet    in_port_t      sin_port;       //port number     struct in_addr sin_addr;     //32 bit IPV4 network byte-order address     char           sin_zero[8];   //Fill Alignment   };   The IPV4 address structure shown in the following figure +posix the data type required by the specification
Universal Socket Address structure
The common socket address structure exists because: when passed into any socket function as a parameter, the socket address structure is always passed as a reference (that is, a pointer pointer to the struct). Any socket function that takes such a pointer as one of the arguments must handle the supported The socket address structure of any protocol family. Different protocols have different socket address structures, and the parameters of the function how to declare the pointer type of the socket address structure is a problem, so a universal socket address structure is defined. All functions that require a socket address structure to be parameters are declared to be types of pointers to this generic socket address structure. Pointers to other socket address structures are cast to the pointer type of the generic socket address structure. From the perspective of application development, The only use of these generic socket structures is to cast a generic socket address structure on a pointer execution type that points to a protocol-specific socket address structure as follows:
[CPP]View Plain copy #include <sys/socket.h>

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.