Network Programming concept full contact (i)
Starting from the most basic concept layer, we can fully understand the essence of the network.
1. Computer network
By connecting the various computer systems with different geographical location and independent functions through communication equipments and lines, the system of resource sharing in network is called computer network with function perfect network software. Computer network is the combination of communication technology and computer technology.
2. Internet (Internet)
The internet, also known as the Internet, is a worldwide network of computer networks of various types and sizes of independent operation and Management. is a global, open information resource network. The internet is a very large and complex computer network.
The internet has three basic elements: TCP/IP (Transmission Control Protocol/Internet Protocol), DN (domain name) and IP address, URL (Uniform Resource Locator).
3. Network protocol
Network protocol is a network (including the Internet) transmission, management of information norms. Just as people communicate with each other as they need to follow certain rules, mutual communication between computers requires a common rule, which is called network protocol. It is a kind of "language" that computers communicate with each other through the network. The same network protocol must be used between different computers to communicate. Computers on the network need to communicate with each other and must follow certain protocols. Common network protocols are tcp/ip,ipx/spx and NetBEUI. The most widely used network protocol currently is the TCP/IP protocol for the Internet.
4.IP Address
IP is the abbreviation of Internet Protocol, namely Internet Protocol. In order to accurately locate a computer on the network, the TCP/IP protocol assigns to each computer connected to the Internet a unique 32-bit binary digit address, which is what we often call an IP address. Each host on the Internet has a unique IP address, which is the basis on which the Internet can run.
The Internet relies on TCP/IP protocol to realize the interconnection of different hardware structures, different operating systems and different network systems on a global scale. On the Internet, each node relies on a unique IP address to differentiate and relate to each other.
5. Domain Name (DN)
Domain name in English, a convenient memory of the IP address alias, but also the equivalent of a business or organization on the Internet host a code. The form of a domain name is made up of several English letters and numbers, by "." Separated into several. such as: www.sohu.com.cn
6.URL (Uniform Resource Locator)
The URL is the abbreviation of Uniform Resource Locator, that is, the unified resource positioning system, which is what we usually call the URL. A URL is a presentation that specifies the location of information on the Internet's WWW service program, specifying Internet protocols such as HTTP or FTP, and is the only naming convention that recognizes the location of specific computers, directories, or files on the Internet.
For example, the URL of a forum is: Http://www.test.com/bbs
7. Network programming
The purpose of network programming is to communicate with other computers directly or indirectly through network protocols. There are two main problems in network programming, one is how to accurately locate one or more hosts on the network, and the other is how to reliably and efficiently transmit data after finding the host. In the TCP/IP protocol, the IP layer is mainly responsible for the location of the network host, the routing of Data transmission, the IP address can uniquely determine a host on the Internet. The TCP layer provides reliable or unreliable data transmission mechanism for application, which is the main object of network programming, and generally does not need to care about how the IP layer handles the data.
At present, the more popular network programming model is client/server (c/s) structure. That is, both sides of the communication wait for the customer to request and respond as a server. The customer applies to the server when the service is needed. The server is always running as a daemon, listening to the network port, once the customer request, will start a service process to ring should customers, while their own continue to monitor the service port, so that later customers can also be timely access to services.
IP address and host name on the Internet is one by one corresponding, through the domain name resolution can be the host name of the machine IP, because the machine name closer to the natural language, easy to remember, so use than IP address a wide range, but for the machine only IP address is a valid identifier.
There are usually a number of processes on a single host that require network resources for network traffic. The object of network communication is not the host, but the process running in the host. It is obviously not enough to have a host name or IP address to identify so many processes at this time. The port number is a means to provide more network resources on one host, and also a mechanism provided by the TCP layer. Only a combination of host name or IP address and port number can uniquely determine an object in network traffic: a process.
8. Sockets
The so-called socket is also commonly called "socket", used to describe the IP address and port, is a communication chain handle. The application usually sends a request to the network through a "socket" or answers a network request.
Sockets can be categorized according to the nature of the communication, which is visible to the user. Applications typically communicate only between sockets of the same class. However, as long as the underlying communication protocol allows, the different types of sockets can still be communicated. There are two different types of sockets: Stream sockets and datagram sockets.
Sockets are the cornerstone of communication, and are the basic operating unit of network communication that supports TCP/IP protocol. Sockets can be viewed as endpoints of two-way communication between different host processes, which form the programming interface between a single host and the entire network. Sockets exist in the communication domain, which is an abstract concept that is introduced in order to handle a generic thread through socket communication. Sockets typically exchange data with sockets in the same domain (data interchange may also traverse the bounds of the domain, but some interpreter must be executed at this point). Various processes use this same domain to communicate with each other using an Internet Protocol cluster.
9. Socket working principle
To communicate over the Internet, you need at least a pair of sockets, one running on the client side, what we call Clientsocket and the other running on the server side, which we call ServerSocket.
The connection between sockets can be divided into three steps, depending on how the connection is started and the destination to which the local socket is connected: server listening, client requests, connection confirmation.
The so-called server monitoring, is the server-side sockets do not locate specific client sockets, but in the state of waiting for connection, real-time monitoring network status.
The so-called client request, refers to the client socket to make a connection request, to connect the target is the server-side sockets. To do this, the socket for the client must first describe the socket of the server to which it is connecting, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.
Connection confirmation means that when a server-side socket is heard or received a connection request from a client socket, it responds to the client socket request, creates a new thread, sends the server-side socket description to the client, and once the client confirms the description, the connection is established. While the server-side socket continues to be listening, it continues to receive connection requests from other client sockets.
Network Programming concept full contact (ii)
Network programming is often referred to as socket programming, in the "network programming concept of comprehensive contact one" has done a more detailed explanation. Here we do a practical analysis of the related transmission technologies involved.
One, two types of socket
Two types of sockets: one with "stream Sockets" (streaming format) and the other as "Datagram Sockets" (datagram format).
The datagram format is also known as a connectionless socket, and the reverse stream format must be connected. This is related to the two types of transport protocols used. "Stream Sockets" (streaming format) is based on the TCP protocol, and the "Datagram Sockets" (datagram format) is based on the UDP protocol.
Comparing the difference between UDP and TCP, we can define the meaning of the two types of socket.
Second, TCP and UDP
TCP Transmission Control Protocol Transfer Control protocal
UDP User Datagram Protocol user Datagram Protocol
The difference between TCP and UDP:
TCP UDP
Based on connection based connectionless
system resources require less requirements for multiple system resources
Packet Structure complex packet structure simple
Ensure data correctness may lose package
Guaranteed data order is not guaranteed
High efficiency of communication efficiency relative to low communication
It can be seen that TCP guarantees data reliable and in order to arrive, it is a completely reliable connection transmission, but the resource requirements are also more, the structure is also more complex. TCP first needs to establish a reliable connection at both ends of the transmitter (through three handshake of the signal, detailed reference to the TCP data), and then through the data packet verification to ensure that the data is completely correct. The purpose of UDP design is to send data packages as quickly as possible. So the UDP protocol looks very streamlined. You just create a package, construct an IP header with the target information, and then send it out. No connection required. Of course UDP also need to return through the other "command correct answer" also called "ACK" package to avoid packet loss. If within a certain time, the sender does not receive a reply from the receiver, resend
Packets until an ACK acknowledgement package is received.
For example, chat rooms use the TCP protocol to transmit text, control information, artboards, and screen broadcast data, and use UDP protocols to transmit voice and video streams.
Third, the transmission of data packets on the network
The famous network seven layer model OSI/RM believe many people know.
Application Layer (application)
Presentation Layer (presentation)
Conversation Layer (session)
Transport Layer (transport)
Network Layer (Network)
Data Link Layer
Physical Layer (physical)
TCP/IP four-layer model
Application layer (Application Layer) (tftp,ftp,telnet)
Transport Layer (Host-to-host transport Layer) (TCP, UDP)
Network layer (Internet Layer) (IP and routing)
Network interface layer (Network Access Layer) (Network layer, data link layer and physical layer)
The core idea of the model is the layer encapsulation, the work is independent, the application layer does not need to be about how to work below. Send packets from the top step package, in turn, add the Transport layer header (TCP,UDP), and then add IP header, only to the physical layer, the receiver in turn from the physical layer to start the reverse direction of the packet, until the analysis of the application layer data.
Example: A TFTP package, first (in this case, TFTP) is packaged ("encapsulated") in its header (perhaps the tail), and then the entire data (including the TFTP header) is encapsulated by another protocol (here is UDP), Then next (IP), keep repeating until the hardware (physical) layer (here is Ethernet).
when another machine received the packet, the hardware stripped the Ethernet head, the kernel stripped IP and UDP headers, and the TFTP program stripped the TFTP head and finally got the data.