Android Basics Getting Started tutorial--7.6.1 Socket Learning Network Basics Preparation
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
To take care of beginners who have not learned Java sockets, or to smooth out the concepts related to network protocols involved in Android development,
After all, the interview, the interviewer came to me, say how many layers of network protocol? So, what layer is the IP protocol on? What the hell is a socket?
What kind of different? What layer is the TCP and UDP protocol on? What's the difference ... Well, this ... So the theory of studying the concept of this section is still very
It's necessary! So don't say a word, start this section of the content ~
Analysis of 1.OSI seven-layer network model
Of course, we are not professional network engineering, as long as we know what the layer, it is probably to do what it can be!
OSI layer Seven Network model (bottom to top):
- Physical Layer (physical): Data communication between devices provides transmission media and interconnect devices that provide reliable
Environment. Can be understood as the network transmission of physical media parts, such as NIC, network cable, hubs, repeaters, modems and so on!
At this level, the data is not yet organized, only as raw bitstream or electrical voltage processing, the unit of this layer is:bit bit
- Data Link layer (Datalink): Can be understood as the data channel, the main function is how to do on the unreliable physical circuit
The reliable transfer of data includes: Physical address addressing, data framing, flow control, data checking and re-sending, etc.
In addition, this data link refers to the physical layer to provide transmission media and its connection to the data communication between the terminal devices. Media is
Long-term, the connection has the survival period. During the lifetime of the connection, the two 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 set up data and send and receive relations ~
The device of this layer is: network card, Network Bridge, network switch , and the unit of this layer is: frame
- Network layer: The main function is to translate the network address into the corresponding physical address, and decide how to send the data from the
The sending party is routed to the receiver, so-called Routing and Tracing: a terminal may need to communicate with multiple terminals, resulting in a
The problem of linking any two end-device data together! Simply put: Set up a network connection and provide services for the upper layer!
The devices on this layer are: route ! The units of this layer are: packets , and the IP protocol is on this level!
- Transport Layer (Transport): Provides communication services to the application layer above, toward the highest level of the communication section, and also
The lowest layer in the user function. Receives session-level data, splits the data when necessary, and gives the data to the network
layer, and ensure that these data segments are valid for reaching to the end! So the unit of this layer is: data segment , and this layer has two very important
Protocol is:TCP transmission Control protocol and UDP user datagram Protocols , which is also part of the core of this chapter!
- Session Layer: Responsible for establishing, maintaining, and terminating communication between two nodes in the network. Establish communication links,
Keep communication links open during sessions, synchronize conversations between two nodes, determine if communication is interrupted, and when communication is interrupted
Decide where to resend from, that is, the creation and management of conversations between users on different machines!
- presentation Layer (Presentation): interprets commands and data from the application layer, assigning various syntaxes to the appropriate
and is routed to the session layer in a certain format. Its main function is "to handle the representation of user information, such as coding,
Data format conversion and encryption decryption, compression decompression ", etc.
- Application Layer (application): The highest layer of the OSI Reference Model, which provides network services for the user's application.
Based on the other 6-tier work, it is responsible for completing the connection between the application and the network operating system in the network, establishing the connection with the end user, and completing various protocols such as the Network Service and the supervision, management and service required by the network users. In addition, the layer is responsible for coordinating the work between the various applications. The services and protocols that the application layer provides to users are: File services, directory services, File transfer Service (FTP), Remote Login service (telnet), e-mail Service (email), Print service, security Service, network Management Service, database service, and so on.
All right, here we have a brief overview of the OSI seven-layer network model, which is summarized below:
The OSI is an ideal model in which the general network system involves only a few layers, and in the seven-tier model, each layer provides a special
Network function, from the point of view of network function:
- The following 4 layers (physical layer, Data link layer, network layer and Transport layer) mainly provide data transfer and exchange functions,
That is, node-to-node communication is the main
- The 4th layer, as a bridge between the upper and lower parts, is the most critical part of the whole network architecture.
- The upper 3 layers (Session layer, presentation layer, and application layer) provide the information and data processing functions between the user and the application.
In short, the next 4 layers mainly complete the function of the communication subnet, the upper 3 layer mainly completes the function of the resource subnet.
--The above reference is from: The OSI seven layer model detailed
2.TCP/IP four-layer model
TCP/IP is synonymous with a set of protocols, and it also includes many protocols that make up the TCP/IP protocol cluster.
TCP/IP protocol cluster is divided into four layers, IP is located in the second layer of the Protocol cluster (corresponding to the third layer of OSI), TCP is located in the third layer of the Protocol cluster
(corresponding to the fourth layer of the OSI). The TCP/IP protocol uses a 4-tier hierarchy, each of which calls to its next level to provide
Network to fulfill their needs. These 4 layers are:
- application Tiers : Layers of communication between applications, such as simple e-mail Transfer (SMTP), File Transfer Protocol (FTP),
Network Remote Access Protocol (Telnet), and so on.
- Transport Layer : In this layer, it provides data transfer services between nodes, such as Transmission Control Protocol (TCP),
User Datagram Protocol (UDP), and so on, TCP and UDP add data to the packet and transfer it to the next layer,
This layer is responsible for transmitting the data and determining that the data has been delivered and received.
- Network Interconnect Layer : responsible for providing basic data packet transfer function, so that each packet can reach the target
Host (but not check whether it is received correctly), such as Internet Protocol (IP).
- host-to-network layer : Management of the actual network media, defining how the actual network is used
(such as Ethernet, Serial line, etc.) to transmit data.
3.TCP/UDP Difference Explanation
Well, the first two points to talk about, just for everyone to popularize the OSI seven layer model and the TCP/IP four Layer model concept, the next thing to say is
Some concept nouns related to our socket development!
1) IP Address
2) port
1. used to differentiate different applications
2. The port number range is 0-65535, where 0-1023 is not the reserved port of the system, our program do not use these ports as far as possible!
3. the IP address and port number make up our socket,socket is the end point of the bidirectional communication link between the network running program,
Is the basis of TCP and UDP!
4. ports Used by common protocols: http:80,ftp:21,telnet:23
3) Comparison of TCP protocol and UDP protocol:
A detailed description of the TCP protocol process :
First TCP/IP is a protocol cluster, which includes many protocols. UDP is just one of them. is named the TCP/IP protocol,
Because the TCP,IP protocol is two very important protocols, it is named after him.
Let's explain the difference between the TCP protocol and the UDP protocol:
TCP(transmission Control Protocol, transmission Protocol) is a connection-oriented protocol, that is, when sending and receiving data
, you need to establish a reliable link to the opposite, this is also the interview will often ask the TCP three handshake and TCP four waves !
three-time handshake :
When establishing a TCP connection, it is necessary for the client and server to send a total of 3 packets to confirm the establishment of the connection.
In socket programming, this process is triggered by the client executing connect, with the following flowchart:
- First Handshake : The client will set the flag bit SYN to 1, randomly generate a value of seq=j, and send the data packets to the server,
The client enters the Syn_sent state and waits for the server to confirm.
- second handshake : After the server receives the packet by the flag bit syn=1 knows the client request to establish a connection, the server will flag the bit
Both the SYN and ACK are set to 1,ack=j+1, randomly producing a value of seq=k and sending the data packets to the client to confirm the connection request
, the server enters the SYN_RCVD state.
- third handshake : After the client receives the acknowledgment, check whether the ACK is j+1,ack 1, and if correct, the flag bit ACK
Set to 1,ack=k+1, and packets the data to Server,server to check if the ACK is k+1,ack 1, if correct
The connection is established successfully, the client and server enter the established state, complete three handshake, and then between the client and the server can
Started transmitting data.
four times Wave :
Terminating a TCP connection means that when a TCP connection is disconnected, a total of 4 packets are sent to the client and the server to confirm the disconnection of the connection.
In socket programming, this process is triggered by executing close on either side of the client or service side, with the following flowchart:
- First wave : client sends a fin to shut down client to server data transfer, client enters
Fin_wait_1 Status
- Second Wave : After receiving fin, the server sends an ACK to the client, confirming that the sequence number is received sequence number +1 (same as SYN,
A fin occupies a serial number) and the server enters the close_wait state.
- Third Wave : server sends a fin to shut down server-to-client data transfer, Server enters Last_ack
State.
- The Fourth Wave : After the client receives fin, the client enters the TIME_WAIT state, then sends an ACK to the server, confirming the serial number to receive the serial number +1,server Enter the closed state, four times to complete the wave.
It may also be the case of a colleague initiating an active shutdown:
There may also be a common problem: why is it that the connection is three handshakes, while the connection is four waves?
A: The ACK and SYN are placed in a message after receiving a SYN message that establishes the connection request, because the server is in the listen state.
Sent to the client. When the connection is closed, when the other's fin message is received, it simply means that the other party no longer sends the data but also
Can receive data, your own may not all the data are sent to each other, so you can immediately close, you can also send some
Data to each other, then send the fin message to the other side to express the consent to now close the connection, therefore, their ACK and fin will generally
Sent separately.
The UDP protocol is detailed :
UDP (User Datagram Protocol) Subscriber Datagram Protocol, non-connected protocol, which transmits data before the source and terminal are not
Establish a connection that simply crawls data from the application when it wants to teleport and throws it to the network as quickly as possible.
On the sending side, UDP transmits data only at the speed of application-generated data, the ability of the computer, and the transmission bandwidth
At the receiving end, UDP places each message segment in the queue, and the application reads a message segment from the queue each time.
Compared to TCP is no need to establish a link, simple structure, can not guarantee correctness, easy to lose packets
--part of the above is excerpted from:
TCP/IP three-time handshake with four waves
The difference between TCP and UDP (GO)
The 4.Java provides several key classes for the network:
For different levels of network communication, Java provides us with four types of network functions:
- inetaddress:
Used to identify hardware resources on the network
- URL:
Uniform Resource Locator, which can read or write data directly to the network via a URL
- sockets and ServerSocket:
Socket-related classes for network communication using the TCP protocol
- Datagram:
Use the UDP protocol to save data in a datagram and communicate over the network
In this section we will cover only the first two classes, and the sockets and datagram to the TCP and UDP sections again!
examples of ~inetaddress use :
Example code :
Public classinetaddresstest { Public Static void Main(string[] args) throws exception{//Get instances of native inetaddress:InetAddress address = Inetaddress.getlocalhost (); System. out. println ("native name:"+ Address.gethostname ()); System. out. println ("IP address:"+ address.gethostaddress ());byte[] bytes = Address.getaddress (); System. out. println ("IP address as a byte array:"+ arrays.tostring (bytes)); System. out. println ("Direct Output InetAddress object:"+ address); }}
run the result diagram :
~url: This is needless to say, forget to see the previous HTTP protocol explained there ~
This section summarizes:
This section is full of concepts, it seems to be able to choke is put, but do not understand it's okay to know that seven layer model each layer called
What, about what to do, there are TCP three handshake and four waves, you can! Of course, it's just to
Face to face interview ~ Actual development We're going to have a tangle of this ... Direct socket Yes ~ Well, next verse we're going to start learning
Socket Communication in Android ~ Thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--7.6.1 Socket Learning Network Basics Preparation