Socket directory
Introduction to Socket Communication
There are two main communication methods between Android and the server, one is Http communication and the other is Socket communication. The biggest difference between the two is that the http connection uses the "request-response method", that is, a connection channel is established at the time of the request, and the server can only return data to the client after the client sends a request to the server. Socket communication, on the other hand, can directly transmit data after the two parties establish a connection, and can realize the active push of information during the connection, without the need for the client to send a request to the server every time. So, what is a socket? Socket, also known as socket, provides a port for communication with the outside world within the program, that is, port communication. By establishing a socket connection, a channel can be provided for data transmission between the two communicating parties. The main features of socket are low data loss rate, simple use and easy portability.
What is Socket
Socket is an abstraction layer through which applications send and receive data. Using Socket, applications can be added to the network and communicate with other applications in the same network. Simply put, Socket provides a port for communication between the program and the outside world and provides a data transmission channel for both parties.
1:Socket
There are two main types of usage:
- Stream socket (
streamsocket
): Based on theTCP
protocol, it provides reliable byte stream service by stream - Datagram socket (
datagramsocket
): Based on theUDP
protocol, it uses datagrams to provide data packaged and sent services
2: Communication Model Diagram
Comparison of Socket and Http
Socket
It belongs to the transport layer, because theTCP / IP
protocol belongs to the transport layer and solves the problem of how data is transmitted in the networkHTTP
The protocol belongs to the application layer, and the solution is how to wrap the data
Since the two do not belong to the same level, they are not comparable. But with the development, the default Http encapsulates the use of the following layers, so there will be a comparison of Socket
& HTTP
protocols: (mainly due to the difference in working methods):
Http
: The request-response method is used.
- That is, after the network connection is established, the server can return data to the client only after the client sends a request to the server.
- It can be understood as: it is the client who needs to communicate
Socket
: The server actively sends data
- That is, after the network connection is established, the server can actively send messages to the client without the need for the client to send a request to the server.
- It can be understood as: it is necessary for the server to communicate
More related theories can be seen: https://www.jianshu.com/p/a6d086a3997d
References:
https://www.jianshu.com/p/a6d086a3997d
https://www.cnblogs.com/zhujiabin/p/5675716.html
https://blog.csdn.net/carson_ho/article/details/53366856