Development must understand the network common sense

Source: Internet
Author: User

1, network commonly used concepts

The web is the soul of application and the source of data for all applications. Away from the network, our application is backwater, the network is the media of data interaction, we get the data on the server through the network, but also pass the data to the server through the network. In other words, the essence of the network is interactive data, and the media of data interaction is the transmission of binary data stream (including html/image/video data) in the network.

Common concepts of Internet development:

Client: Mobile device (handset/ipad and other handheld devices), the client is usually front-end/foreground and so on. Ios,android development is the front-end development.

Server: A machine that provides services to clients (such as data/resources, etc.)---nature is also a computer (+ Server software), and server development is backend/backend development. Java/php/.net and so on.

Request: The client requests data from the server.

Response (Response): The response of a server to a client request is typically to return data to the client.

Server: According to the development phase, divided into two types:

Remote server: An extranet server. The server to be used by all users after the application is on-line. Speed depends on the user's Internet connection and server performance.

Local server: Intranet server, test server. The server used to develop the test phase. For internal development testers to use. It's fast.

2. What is an IP address

Static IP Address: a fixed IP address that needs to be set manually by the user.

Dynamic IP Address: When the computer is turned on, the automatically generated IP address is not set manually. The dynamic IP address is automatically obtained through the DHCP protocol implementation.

Subnet mask: Use with IP addresses to determine whether two computers are on the same subnet.

DNS server: You can convert a domain name (URL) to an IP address.

DHCP protocol: In each sub-network, one computer is responsible for managing all IP addresses of this network, which is called a "DHCP server". When a new computer joins the network, the DHCP server must be sent

Send a "DHCP request" packet, request the IP address and the associated network parameters. The DHCP protocol then gives native parameters such as dynamic IP addresses, subnet masks, gateways, DNS servers, and so on.

Ethernet Header: Sets the MAC address of the sender (native) and the receiver (DHCP server). IP header: Sets the IP address of the issuing party (native) and the IP address of the receiver (DHCP server) UDP header: Sets the port and receiver (DHCP server) for the sender (local)  The port.     This section is provided by the DHCP protocol, which is port 68 and the receiver is port 67. So the computer wants to surf the internet, need to determine these four parameters: native IP address, subnet mask, gateway IP address, DNS IP address.

3.HTTP Request

In layman's terms , network protocol is the bridge between network communication and communication, and only computers with the same network protocol can communicate and communicate information. This is like communication between people in the use of the same language, only the use of the same language in order to communicate properly and smoothly. From the professional point of view, the network protocol is the contract that the computer must obey when it realizes the communication in the network, namely the communication protocol. The main is the rate of information transmission, transmission code, code structure, Transmission control procedures, error control and other provisions and set standards.

3.1. URL (Uniform Resource Locator):

Uniform Resource Locator. The URL is the address and location of the resource. You can find only one resource on the Internet through a URL.

Basic format of URL: protocol://HOST address/path

Protocol: Different protocols represent different ways to find resources and how to transfer resources.

Common protocols in URLs:

<1>http: Hypertext Transfer Protocol, the most commonly used protocol in network development. Access to remote network resources. Format:/HTTP/...

&LT;2&GT;IP Protocol:

TCP packets are embedded in IP packets. IP packet headers require both IP addresses to be set. Sender (native) is: 192.168.2.6, receiver (Baidu) is: 119.75.218.70

The header length of the IP packet is 20 bytes. With embedded TCP packets, the total length is now 5000 bytes

<3>file: When accessing a resource on the local computer. Format: file://(do not add host address)

<4>FTP: Access to the shared host's file resources. Format: ftp://

<5> TCP protocol:

The TCP packet header needs to set the port, the receiver (Baidu) HTTP port is 80 by default, and the sender (native) port is an integer between the randomly generated 1024~65535.

The header length of the TCP packet is 20 bytes. Plus the embedded HTTP packet, the total length is now 4980 bytes.

<6> Ethernet Protocol

IP packets are embedded in the data portion of the Ethernet packet. The header of the Ethernet packet needs to set the MAC address of both sides. The sender is the local NIC MAC address, the receiver is the MAC address of the Gateway 192.168.2.1 (via the ARP protocol), the data portion of the Ethernet packet, The maximum length is 1500 bytes, and now the IP packet length is 5000 bytes. Therefore, IP packets must be split into four packets because each package has its own IP header (20 bytes), so the length of the IP packets for four packets is 1500, 1500, 1500,560 bytes, respectively.

<7>mailto: The e-mail address is being accessed. Format: mailto:

Host Address: The host IP address (domain name) where the resource resides.

Path: The resource is in a specific location on the host.

3.2. Complete procedure for HTTP requests:

<1> Request: The client makes the request. Ask for data (operational data) from the server.

<2> response: The server responds to the client's request. Returns the data required by the client.

3.3. Wrapping an HTTP request

Use Nsurlrequest to wrap an HTTP request. You can specify the cache policy and time-out, and select the cache policy: Nsurlrequestcachepolicy

Nsurlrequestuseprotocolcachepolicy =0,        //The default caching policy, using protocol definitions. Nsurlrequestreloadignoringlocalcachedata=1,        //ignore the local cache and download directly from the original server address. Nsurlrequestreturncachedataelseload=2,        //download from the original address only if no data exists in the cacheNsurlrequestreturncachedatadontload=3,        //only cached data is used, and if no cache exists, the request fails. Offline mode for no network connectionNsurlrequestreloadignoringlocalandremotecachedata=4,        //ignore remote and local data caches and download them directly from the original addressNsurlrequestreloadignoringcachedata= Nsurlrequestreloadignoringlocalcachedata =1,        //ignore the cache and download it directly from the original server address.Nsurlrequestreloadrevalidatingcachedata=5,        //Verify that local data and remote data are the same and download remote data if different, otherwise use local data

Network cache data, stored in the SQLite database (Nshomedirectory ()),

3.4. Sending the request

Send the request with Nsurlconnection.

Synchronization method:

+ (NSData *) Sendsynchronousrequest: (nsurlrequest *) Request Returningresponse: (Nsurlresponse *) Response error: ( Nserror * *) error;

Async method:

+ (void) Sendasynchronousrequest: (nsurlrequest*) Request queue: (nsoperationqueue*) queue

Completionhandler: (void (^) (nsurlresponse* response, nsdata* data, nserror* connectionerror)) handler

4.Socket Introduction

The network of two programs through a two-way communication connection to achieve the exchange of data, depending on how the connection starts and the local socket to connect to the target, the connection between sockets can be divided into three steps: Server monitoring, client requests, connection confirmation.

(1) Server monitoring: Is the server end socket does not locate the specific client socket, but in the status of waiting for the connection, real-time monitoring network status. (2) Client request: Refers to the client's socket to make a connection request, to connect to the target is the server-side socket. To do this, the client's socket must first describe the socket of the server it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket. (3) Connection confirmation: When the server-side socket is heard or received a client socket connection request, it responds to the client socket request, set up a new thread, the server-side socket description to the client, 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.

To create a socket in steps

1. Create Client socket socket (< #int #>, < #int #>, < #int #>);

2. Create the server socket struct sockaddr_in serveraddress;

3. Connect to the server (socket programming) Connect (< #int #>, < #const struct sockaddr *#>, < #socklen_t #>);

4. Send data to server send (< #int #>, < #const void *#>, < #size_t #>, < #int #>)

5. Receive the data returned by the server recv (< #int #>, < #void *#>, < #size_t #>, < #int #>)

6. Closing the Socket close (socketnumber)

Development must understand the network common sense

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.