Network programming-TCP/IP basics

Source: Internet
Author: User
Tags connection reset
TCP/IP basics for network programming I. ISO/OSI Reference Model 1. OSI (opensysteminterconnection) open system interconnection model is a network layered model defined by ISO (InternationalOrganizationforStandardization, A total of seven layers .... Network programming-TCP/IP basics
I. ISO/OSI Reference Model 1. OSI (open system interconnection) the open system interconnection model is a layer-7 network layered model defined by ISO (InternationalOrganization for Standardization. 2. q physical layer (PhysicalLayer): the physical layer defines the specifications of all electronic and physical devices and provides a physical medium for upper-layer transmission, the unit of data transmission in this layer is bit ). Standards defined in this layer are EIA/TIA RS-232, EIA/TIA RS-449, V.35, RJ-45, etc., the actual use of equipment such as Nic belongs to this layer. Q data link Layer (DataLink Layer): Performs data frame conversion on the bit streams received by the physical Layer. Provides reliable data transmission services for data transmission without errors. The unit of data in the data link layer is frame ). The specifications defined in this layer include SDLC, HDLC, PPP, STP, and Frame Relay. actually used devices, such as switch switches, belong to this layer. Q Network layer: the network layer is responsible for routing, grouping, and restructuring data between subnets. The unit of data transmission in this layer is packet ). Specifications defined at this layer include IP, IPX, RIP, OSPF, ICMP, and IGMP. In actual use, devices such as routers belong to this layer. Q transport layer: provides reliable data transmission services. it detects discarded packets on the vro and generates a retransmission request to re-sort packets in disorder. Q session layer: manages sessions between hosts, including session creation, termination, and management during sessions. Q presentation layer (PresentationLayer): The presentation layer changes the data transmitted over the network, so that information transmitted between multiple hosts can be understood, including data compression, encryption, and format conversion. Q application layer: the application layer communicates with the application interface for demonstration purposes. Common protocols include HTTP, HTTPS, FTP, TELNET, SSH, SMTP, POP3, etc. layer-2, TCP/IP layer-4 model peer-to-peer communication encapsulation sub-Port q (WellKnown Ports ): from 0 to 1023, these ports are allocated and controlled by IANA and they are closely bound to some services. Usually the communication between these ports clearly indicates a service protocol. For example, Port 21 is the ftp service port. Q registration port (RegisteredPorts): from 1024 to 49151. Some of its ports are not controlled by IANA, but are registered by IANA and provide a usage list. They are loosely bound to some services. That is to say, many services are bound to these ports, which are also used for many other purposes. For example, 1433 Microsoft SQL service port q dynamic or Private port (Dynamicor Private Ports): from 49152 to 65535. IANA does not care about these ports. In fact, machines usually allocate dynamic ports from 1024. But there are also exceptions: SUN's RPC Port starts from 32768. 02tcp/ip basics (ii) I. MTU Ethernet and IEEE 802.3 have limits on the data frame length. The maximum values are 1500 and 1492 bytes, respectively, this restriction is called the Maximum Transmission Unit (MTU, Maximum Transmission Unit). if there is a datagram on the IP layer, and the data length is greater than the MTU on the link layer, in this case, Fragmentation is required on the IP layer to split the datagram into several slices so that each slice is smaller than MTU. When two hosts on the network communicate with each other, the two hosts need to go through multiple networks, and the link layer of each network may have different MTU, the minimum MTU in the two communication host paths is called the path MTU. II. Ethernet frame format 3. ICMP 4. ARP 5. data transmission process in the network step a: ping the application to determine whether the sent host name or IP address is used. call the gethostbyname () function () resolve host machine B and convert the host name to a 32-bit IP address. This process is called DNS domain name resolution step B: The ping program sends an ICMP ECHO packet to the destination IP address step c: convert the IP address of the destination host to a 48-bit hardware address, send an ARP Request broadcast in the Lan to find the hardware address of host B. Step d: After the ARP protocol layer of host B receives the ARP request from host A, it fills in the hardware address of the local machine in the response packet and sends the ARP response to host. Step e: send an ICMP packet to host B step f: host B receives an ICMP packet from host A and sends A response packet. Step g: Host A receives the ICMP packet response packet from host B. VI. RARP 03tcp/ip basics (iii) I. IP datagram format 1. version a) the IP protocol version number. the length is 4 bits, the IPv4 field value is 4, and the IPv6 field value is 62. header Length a) is measured in 32 characters. the length of this field is 4 characters and the maximum value is 15. Therefore, the header length is up to 60 bytes. the length of service type (TOS) a is 8 bits. This field contains three-bit priority (ignored now), four-bit service type subfields, and one-bit reserved bits (must be set to 0 ). The four-bit service types are minimum latency (D), maximum throughput (T), maximum reliability (R), and minimum cost (F ).. Total length a) the length of this field is 16 bits, in bytes. the length of this field contains the IP header and data section. An IP datagram can contain up to 65535 bytes. 6. ID a) a 16-bit identifier used to identify an IP package. each time a packet is sent, a value of 17 is added. the first digit is not used. if the second digit is DF (Don't fragment), if it is 1, if the transmitted datagram exceeds the maximum transmission unit, the datagram is discarded and an ICMP error message is sent. The third-digit MF (More Fragment) indicates whether More slices exist. The value of this parameter is 1, indicating that More fragments exist in the future. The last MF is 0. B) after an IP segment is split, each group has its own header, but the segment offset value is different. the receiver can re-assemble the IP packet through the segment offset value. 8. TTLa TTL (Time To Live) indicates the maximum number of routers that can pass through a datagram. Each time a datagram passes through a vro, the TTL is reduced by 1, and 0 is dropped, and an ICMP message is sent to notify the source host. TTL can avoid continuous circulation of data between routers. 9. Protocol Type a) indicates which advanced protocol is hosted on the IP layer. In the process of encapsulation and sub-use, the protocol stack knows which layer the protocol should be handed over. 1 ICMP 2 IGMP 6 TCP 17 UDP10. header checksum (a) ensures data integrity in the datagram header, but does not include data. The purpose of this solution is as follows: first, all high-level protocols that encapsulate data in IP data packets contain a checksum that overwrites the entire data, therefore, it is not necessary to verify the data part carried by the IP datagram. Second, each time a router passes through, the IP datagram header must be changed, while the data part remains unchanged. in this way, only the changed header is verified, which obviously does not waste much time. In order to reduce the computing time, the CRC verification code is generally not used, but a simpler Internet Checksum is used. 11. TTLa TTL (Time To Live) indicates the maximum number of routers that can pass through a datagram. Each time a datagram passes through a vro, the TTL is reduced by 1, and 0 is dropped, and an ICMP message is sent to notify the source host. TTL can avoid continuous circulation of data between routers. 12. Protocol Type a) indicates which advanced protocol is hosted on the IP layer. In the process of encapsulation and sub-use, the protocol stack knows which layer the protocol should be handed over. 1 ICMP 2 IGMP 6 TCP 17 UDP13. header checksum a) to ensure the data integrity of the datagram header, but the validation does not include the data part. The purpose of this solution is as follows: first, all high-level protocols that encapsulate data in IP data packets contain a checksum that overwrites the entire data, therefore, it is not necessary to verify the data part carried by the IP datagram. Second, each time a router passes through, the IP datagram header must be changed, while the data part remains unchanged. in this way, only the changed header is verified, which obviously does not waste much time. In order to reduce the computing time, the CRC verification code is generally not used, but a simpler Internet Checksum is used. 14. source IP address a) host IP address for sending data 15. destination IP address a) host IP address for receiving data 16. option and fill (option is 4-byte integer times, otherwise fill with 0) a) security and processing limit B) path record: record the IP address of the experienced router c) timestamp: record the IP address and time of the router. d) Kuan song origin site route: specify the IP address that the data packet must go through. E) strict source route: specifies the IP address that a data packet must go through. it cannot go through an unspecified IP address. 2. Internet Checksum (Internet Checksum) the sender divides the data to be sent into many 16-bit character sequences (if the number of data bytes is odd, add one byte 0 at the end to make it an even number .) The checksum is obtained by summation of the reverse codes of these sequences. If no error occurs during data transmission, the receiver calculates the checksum result as full 1. If the result is not all 1 (that is, the checksum is incorrect ). III. route 04tcp/ip basics (IV) I. TCP features basically in byte stream oriented connection reliable transmission buffer transmission full duplex traffic control II. TCP packet segment format 1. SOURCE port number and destination port number a) source port number and destination port number, and the source IP address and destination IP address of the IP header uniquely determine a TCP connection. 2. serial number a) serial number indicates the first data byte number in the packet segment. 3. Confirmation No. a) valid only when the ACK mark is 1. The confirmation number indicates the sequence number of the next byte to be received. 4. header Length a) 4 bits, TCP header up to 60 bytes 5. reserved a) 6 bits, must be 06. 6 Flag a) URG-Emergency pointer valid B) ACK-confirm serial number valid c) PSH-the receiver should send this packet segment to application layer d as soon as possible) RST-connection reset e) SYN-synchronous sequence number is used to initiate a connection f) FIN-indicates that a connection is to be terminated 7. window size 8. the window size is used to control the traffic. 9. checksum 10. check the tcp header and data 11. emergency pointer a) is a positive offset. the sum of the values in the serial number field indicates the sequence number of the last byte of the emergency data. The TCP emergency mode is a method in which the sender sends emergency data to the other end. 12. options and fill (options are 4-byte integers, otherwise fill with 0) a) the most common optional field is the Maximum message Size (MSS (Maximum Segment Size ), each connector usually specifies this option in the first packet segment of the communication. It specifies the maximum length of packets that can be received by the local end. If this option is not set, the default value is 536 (20 + 20 + 536 = 576 bytes of IP datagram) 3. establish a three-way handshake for a connection 4. terminate a connection 4. how can TCP ensure reliability the application data is split into data blocks that TCP considers to be the most suitable for sending, which are called segments passed to the IP layer. When TCP sends a segment, it starts a timer and waits for the destination to confirm receiving the segment. If a confirmation message cannot be received in time, the message segment will be resold. When TCP receives data from the other end of the TCP connection, it sends a confirmation message. This confirmation is not sent immediately. it will usually be postponed by a few seconds. TCP will keep its header and data checksum. This is an end-to-end checksum to detect any changes in data during transmission. If the verification and error of the received segment are received, TCP will discard the packet segment and uncertain (resulting in timeout retransmission by the other party) the TCP will carry the IP datagram for transmission, while the arrival of the IP datagram may be out of order, therefore, the arrival of TCP packets may be out of order. TCP sorts the received data again. Duplicate IP datagram occurs, and the TCP receiver must discard duplicate data. TCP can also provide traffic control. Each side of a TCP connection has a buffer space of a certain size. 05tcp/ip basics (5) I. sliding window protocol announcement receiving window (rwnd): prevents applications from sending data beyond the recipient's buffer. The traffic control congestion window (cwnd) used by the receiver: prevents the data sent by the application from exceeding the network's capacity. The sender uses the traffic control sending window to take a small value of the two slow start threshold values (ssthresh: slow start threshold) slow start phase: cwnd increases exponentially from 1 until ssthresh congestion avoidance phase: cwnd increases linearly until it is congested, and CND = 1 and ssthresh are halved.
Related Article

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.