Python Network programming--socket

Source: Internet
Author: User
Tags ack snmp file transfer protocol

First, the basic knowledge of the network

1. OSI Reference Model

The OSI is an abbreviation for open System Interconnect, which means that it is connected to the opening systems. Generally called the OSI Reference Model, it is the network interconnection model that the ISO organization studied in the 1985. The architecture standard defines a seven-tier framework for network interconnection, under which each layer of functionality is further elaborated to achieve interconnection, interoperability, and portability of applications in an open system environment.

The various levels of the OSI seven-layer Reference model follow the following principles:

1, each network node in the same layer has the same hierarchy structure, has the same function.

2, the adjacent layer within the same node through the interface (can be a logical interface) to communicate.

Each layer in the 3, seven-tier structure uses the services provided on the next tier and provides services to the upper layer.

4, different nodes of the same layer in accordance with the Protocol to achieve the communication between peer layer.

Physical LayerThe physical layer is the first layer of the OSI, and although it is at the bottom, it is the foundation of the entire open system. The physical layer provides transmission media and interconnect equipment for data communication between devices, providing a reliable environment for data transmission. 1.1 Media and interconnection equipment specifies the mechanical, electrical, functional, and process characteristics of the communication equipment used to establish, maintain and dismantle physical link connections. Specifically, the mechanical characteristics of the network connected to the required plug-in specification size, PIN number and arrangement of the situation, etc. the electrical characteristics specify the size of the signal level on the line, the impedance matching, the transmission rate distance limit and so on when the bit stream is transmitted on the physical connection. The physical layer of media includes overhead wire, balanced cable, optical fiber, wireless channel and so on. Interconnection devices for communications refer to the interconnection devices between DTE and DCE. DTE is the data terminal equipment, also known as physical devices, such as computers, terminals, etc. are included. The DCE is a data communication device or circuit connection device, such as a modem. The data transfer is usually through DTE--DCE, then through the Dce--dte path. Interconnection devices are devices that connect DTE and DCE, such as various plugs and sockets. Various coarse and fine coaxial cables, T-connectors, plugs, receivers, transmitters, repeaters, etc. in LAN are all media and connectors of the physical layer. 1.2 The primary function of the physical layer 1.2.1 provides a path for data-end devices to transmit data, which can be either a physical medium or multiple physical media connections. A complete data transfer, including activating the physical connection, transmitting the data, and terminating the physical connection. The so-called activation, that is, regardless of how much physical media participation, should be in the communication between the two data terminal equipment connected together to form a path. 1.2.2 Transfer data, the physical layer to form a suitable entity for data transmission needs, for the transfer service. The first is to ensure that the data can be passed correctly on it, and the other is to provide enough bandwidth (the bandwidth is the number of bits per second that passes through) to reduce congestion on the channel. The way data is transferred can be met point-to-point, point-to-multipoint, serial or parallel, half-duplex or full-duplex, synchronous or asynchronous transmission. The   Data link layer data link can be roughly understood as a data channel. The physical layer provides the transmission medium and its connection to the data communication between the terminal devices. The media is long-term, the connection has the survival period. During the lifetime of the connection, Both ends of the transceiver can be unequal to one or more data communication. Each communication must be established through communication and the removal of communication between the two processes. This established data-sending relationship is called a data link. And the data transmitted on the physical media are inevitably affected by various unreliable factors, in order to compensate for the deficiencies in the physical layer , to provide error-free data transmission to the upper layer, it is necessary to be able to detect errors and error correction. Data link building, demolition, data detection, error correction is the basic task of data link layer. The main function link layer of the 2.1 link layer is to provide data transfer service for the network layer, which relies on the functions of this layer toRealize. The link layer should have the following functions: Physical address addressing, data framing, flow control, data error checking, re-sending, etc. In this layer, the units of the data are called frames. 2.2 The main protocol of Data link layer the Data Link layer protocol is made for consistency among the peer entities, and also to successfully complete the service to the network layer. The Data Link layer protocol includes: SDLC, HDLC, PPP, STP, Frame Relay, and so on. The generation of network layer of   network layer is also the result of network development. In an environment where online systems and line switching are in place, the functionality of the network layer does not make much sense. When the data terminal increases. There are relay devices connected to each other. At this point, a terminal requires more than the only one, but can communicate with multiple terminals, which is the production of any two data terminal equipment data link up the problem, that is, routing or search path. In addition, when a physical channel is established and used by a pair of users, there is often a lot of idle time wasted. People will naturally want Jeandot to share a link to the user, in order to solve this problem there is a logical channel technology and virtual circuit technology. 3.1 Network layer The main function of the network layer of the task is to select the appropriate network routing and Exchange nodes, to ensure the timely delivery of data. The network layer consists of a packet of frames provided by the data link layer, which encapsulates the network layer header, which contains the logical address information-the network address of the source site and destination site address. If you're talking about an IP address, then you're dealing with layer 3rd, which is the "packet" problem, not the 2nd "frame". IP is part of the 3rd tier problem, and there are routing protocols and address resolution protocols (ARP). Everything about routing is handled in this 3rd layer. Address resolution and routing are important purposes of layer 3. Network layer can also realize congestion control, Internet interconnection and other functions. At this level, the unit of data is called a packet (packet). Network layer protocol representatives include: IP, IPX, RIP, OSPF, and so on. The data unit on the 4th layer of the   Transport layer is also known as a packet (packets). However, when you talk about specific protocols such as TCP, there are special terms, the TCP data unit is called segment (segments) and the UDP protocol data unit is called "Datagram (datagrams)". This layer is responsible for getting all the information, so it must keep track of the fragmentation of data units, the packets arriving in order of chaos, and other risks that may occur during transmission. The 4th layer provides an end-to-end (end-user to end-user) transparent, reliable data transfer service for the upper layers. The transparent transmission refers to the specific details of the transmission layer shielding the communication transmission system in the communication process. The Transport Layer protocol includes: TCP, UDP, SPX, and so on. The services provided by the    session-level session layer enable the application to establish and maintain sessions and to synchronize the sessions. At the session level and above, the unit of data transfer is no longernaming, but collectively referred to as messages. The session layer does not participate in specific transports, it provides mechanisms for establishing and maintaining communication between applications, including access validation and session management. If the server verifies that the user is logged in, it is done by the session layer. The session layer uses checkpoints to enable communication sessions to resume communication from the checkpoint when communication fails. This ability is extremely important for transmitting large files. The session layer, the presentation layer, the application layer constitutes the open system of the high 3 layer, facing the application process to provide distributed processing, dialogue management, information representation, restore the final error, such as .  presentation layer

This layer mainly solves the problem of the grammatical representation of the support information. It transforms the data to be exchanged from the abstract syntax appropriate for a user into the transfer syntax appropriate for internal use within the OSI system. That is to provide a formatted representation and Transform data service. Data compression and decompression, encryption and decryption are the responsibility of the presentation layer.

From the previous introduction, we can see that the following 5 layers of the session layer complete the end-to-end data transfer, and is reliable, error-free transmission. But data transmission is only a means, not an end, and ultimately the use of data. Because the definitions of the data are not exactly the same for various systems, the most obvious example is the keyboard, Some of the keys on it have different meanings in many systems. This naturally creates obstacles to the use of data from other systems. The presentation and application tiers are tasked with eliminating this barrier. The Application Layer Application layer provides an interface for operating system or network applications to access network services. The application layer protocol includes: Telnet, FTP, HTTP, SNMP, and so on. 2. TCP/UDP Agreement

The TCP (transmission Control Protocol) and UDP (User Datagram Protocol) protocols belong to the Transport layer protocol. TCP provides reliable data transmission in IP environment, and it provides services including data transfer, reliability, effective flow control, full duplex operation and multiplexing. Send through connection-oriented, end-to-end, and reliable packets. Popularly said, it is to send the data in advance to create a connected channel, and then send data, and UDP does not provide the IP reliability, flow control or error recovery function. In general, TCP corresponds to the application of high reliability requirements, and UDP corresponding to the requirements of low reliability, transmission economy applications. TCP supports the main application protocols are: Telnet, FTP, SMTP, and so on; The application layer protocols supported by UDP are: NFS (Network File system), SNMP (Simple Network Management Protocol), DNS (primary Domain Name System), TFTP (Common File Transfer Protocol), etc.

TCP/IP protocol is not related to data link layer and physical layer at lower level, which is also an important feature of TCP/IP.

Second, Socket

The socket is essentially a network between 2 computers, set up a channel, two computers through this channel to achieve the transmission of data. We know that network communication is based on the Ip+port can be targeted to the specific machine specific services, the operating system has 0-65535 ports, each port can be independent of external services, if a company than a computer, the company's switchboard number is equivalent to the IP address, Each employee's extension number is equivalent to the port, you want to find someone in the company, you must first call the switchboard, and then ext.

The establishment of a socket must have at least 2 end, a server, a client, the service side passively wait and receive requests, the client initiates the request, after the connection is established, the two sides can send data to each other.

three-time handshake of TCP in the socket to establish a connection

We know that TCP establishes a connection for a "three-time handshake", which is the exchange of three groupings. The approximate process is as follows:

    • The client sends a SYN J to the server
    • The server responds to a SYN K to the client and acknowledges the SYN J ACK j+1
    • The client then wants the server to send a confirmation ack k+1

Only three handshake is done, but this three handshake takes place in the socket of the several functions? Please see:

TCP three-time handshake sent in the socket

As you can see, when the client calls connect , the connection request is triggered, the SYN J packet is sent to the server, then connect enters the blocking state, the server hears the connection request, receives the SYN J packet, calls the accept function to receive the request to send SYN K to the client, Ack J+1, when accept enters the blocking state, the client receives the SYN K of the server, ACK j+1, then connect returns, and the Syn K is confirmed; when the server receives an ACK k+1, the Accept returns, three times the handshake is complete, and the connection is established.

Summary: The client's connect returns in the second time of the three handshake, while the server-side accept is returned for the third time in the three-time handshake.

A network socket is a endpoint of a connection across a computer network. Today, most communication between computers are based on the Internet Protocol; Therefore most network sockets is the Internet sockets. More precisely, a socket was a handle (abstract reference) that a local program can pass to the networking application prog Ramming interface (API) to use the connection, for example "Send this data in this socket". Sockets is internally often simply integers, which identify which connection to use.

For-example, to-send "Hello, world!" via TCP to Port-the host with address 1.2.3.4, one might get a socket, connect It to the remote host, send the string, then close the socket:

1234 socket socket  =  getsocket ( type  =   "TCP" connect (socket, address  =  " 1.2.3.4 " , port   =   " " send (socket,  "Hello, world! " ) close (socket)

socket API  is an application programming interface  (API), usually provided by the operating system, which allows application programs to control and use network sockets. Internet socket APIs is usually based on The berkeley Sockets standard. In the Berkeley sockets, Sockets is a form of file descriptor  (a  file  handle), due To The unix Philosophy that "Everything is a file", and the analogies between sockets and files:you can read, Write, open, and close both. In practice the differences mean the analogy are strained, and one instead use different interfaces (send and receive) on a Socket. In inter-process communication, each end would generally has its own socket, but these could use different apis:they a Re abstracted by the network protocol.

A socket address is the combination of an IP address and a port number, much like one end of a telephone connecti On is the combination of a phone number and a particular extension. Sockets need not has an address (for example for only sending data) and if a program binds a sockets to an Addre SS, the socket can be used to receive data sent to the address. Based on this address, Internet sockets deliver incoming data packets to the appropriate application process or thread.

Python Network programming--socket

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.