1. Concept
1.1 TCP/IP: belongs to the Transport Layer/Network layer protocol. Mobile phone can use the network function is because the bottom of the mobile phone implementation of TCP/IP protocol, you can make the mobile phone terminal through the wireless network to establish a TCP connection. TCP protocol can provide an interface to the upper network, so that the transmission of the upper network data is based on the "No Difference" network. The main solution is the transmission of data in the network.
1.2 http: The Hypertext Transfer Protocol (hypertext Transfer Protocol), which belongs to the application layer protocol, is the foundation of Web networking and one of the most commonly used protocols for mobile Internet, and the HTTP protocol is an application based on TCP protocol. The main solution to data packaging and identification applications.
1.3 Socket: itself is not a protocol, but a calling interface (API). The TCP/IP protocol can be encapsulated and applied as a programming interface for TCP/IP.
2. Model
3. Features
3.1 TCP has three handshakes and four waves. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection.
The most notable feature of the 3.2 HTTP connection is that each request sent by the client requires a server loopback response, and after the request is completed, the connection is actively released. The process from establishing a connection to closing a connection is called a "one-time connection."
The 3.3 socket (socket) is the cornerstone of communication and is the basic operating unit of network communication supporting TCP/IP protocol. It is an abstract representation of the endpoint in the network communication process, including the five kinds of information necessary for network communication: The protocol used by the connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol port of the remote process.
Typically, a socket connection is a TCP connection, and once a socket connection is established, both parties can start sending data content to each other until the two sides are disconnected. Establishing a socket connection requires at least one pair of sockets, one running on the client, called Clientsocket, and the other running on the server side, called ServerSocket.
The connection between sockets is divided into three steps: Server listening, client request, connection acknowledgement.
A Server Listener: Server-side sockets do not locate specific client sockets, but are waiting for the status of the connection, real-time monitoring network status, waiting for the client connection request.
B client request: Refers to the client socket to make a connection request, the destination to connect 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.
C Connection Confirmation: When the server-side socket is heard or received a client socket connection request, in response to the client socket request, set up a new thread, the server-side socket description to the client, once the client confirmed the description, the two sides formally establish a connection. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.
4. HTTP Communication
HTTP (Hypertext Transfer Protocol), first it is a protocol, and is based on the TCP/IP protocol on the basis of the application layer protocol. TCP/IP protocol is the Transport Layer protocol, which mainly solves how data is transmitted in the network, and HTTP is the application layer protocol, which mainly solves how to wrap the data. The HTTP protocol details the rules for communication between the browser and the server, and is the basis for the World Wide Web exchange of information. HTTP is based on the request-response form and is a short connection, and is a stateless protocol. For its stateless characteristics, in the actual application needs to have the form of the state, so generally through the Session/cookie technology to solve this problem.
5. Socket Communication
5.1 Socket based on TCP protocol
Server-side: First declare a ServerSocket object and specify the port number, and then call ServerSocket's Accept () method to receive the client's data. The Accept () method is stuck in a blocked state without data being received. (Socket socket = serversocket.accept ()), once the data is received, the received data is read through InputStream.
Client: Create a Socket object, specify the server-side IP address and port number (socket socket=new socket ("172.168.10.108", 8080), read data through InputStream, Gets the data emitted by the server (OutputStream Outputstream=socket.getoutputstream ()), and finally writes the data to be sent to OutputStream for the socket data transfer to the TCP protocol.
5.2 Data transmission based on UDP protocol
Server-side: First create a Datagramsocket object and the port on which to monitor. Next, create an empty Datagramsocket object to receive the data (Byte data[]=new byte[1024;] Datagramsocket packet=new Datagramsocket (data,data.length)), receives the data sent by the client using the Datagramsocket's Receive method, receive () Similar to ServerSocket's Accepet (), there is no data to be received in a blocked state.
Client: Also creates a Datagramsocket object and directs the port to listen. Next, create an InetAddress object that resembles the sending address of a network (inetaddress serveraddress=inetaddress.getbyname ("192.168.1.120") ). Defines a string to send, creates a Datagrampacket object, and formulates the address and port number to which the datagram packet is sent to the network, and finally sends the data using the Send () of the Datagramsocket object. * (String str= "Hello"; byte Data[]=str.getbyte (); Datagrampacket packet=new datagrampacket (data, data.length, serveraddress,4567); Socket.send (packet);)
6. Expand
6.1 TCP/UDP Difference
TCP is connected; UDP is not connected
TCP-oriented connection; UDP data-oriented
TCP is concerned with returning data; UDP one-time, sending ends
Reliable TCP data transmission (time-out re-sending mechanism; more resources); UDP is relatively unreliable (less resources)
6.2 Http/https Difference
The HTTPS protocol requires a certificate to be applied to the CA, and the general free certificate is very small and requires a fee.
HTTP is a Hypertext Transfer Protocol, the information is plaintext transmission, HTTPS is a secure SSL encryption transport protocol.
HTTP and HTTPS use a completely different connection, the same port, the former is 80, the latter is 443.
The HTTP connection is simple and stateless; The HTTPS protocol is a network protocol built by the SSL+HTTP protocol for encrypted transmission and authentication, which is more secure than the HTTP protocol.
Http://jingyan.baidu.com/article/08b6a591e07ecc14a80922f1.html
http://blog.csdn.net/maoxiao1229/article/details/22886337
http://blog.csdn.net/zeng622peng/article/details/5546384
http://blog.csdn.net/lanhuzi9999/article/details/32713815
Android from TCP/IP, HTTP see socket communication