- -There are two main ways of communication between Android and server, one is HTTP communication and one is socket communication. The biggest difference between the two is that the HTTP connection uses "request-response mode", which is to establish a connection channel on request, and the server can return the data to the client after the client sends a request to the server. And the socket communication is to establish a connection between the two can be directly transmitted data, the connection can realize the active push of information, and do not need each time by the client to send a request to the server. So, what is a socket? Socket, also known as socket, within the program provides a port to communicate with the outside world, that is, port communication. By establishing a socket connection, a channel can be provided for transmitting data from both sides of the communication. The main features of the socket are low data loss rates, simple to use and easy to migrate.
- -What is a socket: an abstraction layer through which applications send and receive data, using sockets to add applications to the network and communicate with other applications that are on the same network. Simply put, the socket provides a port within the program that communicates with the outside world and provides a data transfer channel for both sides of the communication.
- -In many cases, the server side is required to proactively push data to the client, keeping the client and server data in real time and in sync. At this point, if the two sides established a socket connection, the server can directly transfer the data to the client, if the two sides establish an HTTP connection, the server needs to wait until the client sends a request before the data can be sent back to the client, so the client periodically sends a connection request to the server, not only to remain online, It also asks the server if there is any new data, and if so, it passes the data to the client.
- S ocket Communication and HTTP methods each have advantages and disadvantages, it is difficult to say which is better. But in general, the use of HTTP links is already possible. Unless it's a real-time application, use HTTP to
- -depending on the underlying protocol, the socket is implemented in a variety of ways. This guide describes only the contents of the TCP/IP protocol family, where the main socket types are stream sockets (Streamsocket) and datagram Sockets (Datagramsocket). The stream socket provides a trustworthy byte-stream service for TCP as its end-to-end protocol. Datagram sockets use the UDP protocol to provide data packaging and delivery services. Let's take a look at the basic implementation model of these two socket types.
- Second, Socket basic communication model
Three, the basic principle of socket implementation
3.1 Socket based on TCP protocol
The server-side first declares a ServerSocket object and specifies the port number, and then calls ServerSocket's accept () method to receive the client's data. The Accept () method is stuck in a blocked state without data being received. (Socketsocket=serversocket.accept ()), once the data is received, the received data is read by InputStream.
The client creates a socket object that specifies the server-side IP address and port number (Socketsocket=newsocket ("172.168.10.108", 8080), and reads the data through InputStream, Gets the data emitted by the server (Outputstreamoutputstream=socket.getoutputstream ()), and finally writes the data to be sent to the OutputStream for the socket data transfer of the TCP protocol. 3.2 Data transmission based on UDP protocol
The server-side first creates a Datagramsocket object and directs the port to listen. Next, create an empty Datagramsocket object to receive the data (bytedata[]=newbyte[1024;] Datagramsocketpacket=newdatagramsocket (Data,data.length)) to receive data sent by the client using Datagramsocket's Receive method, Receive () is similar to ServerSocket's Accepet () in a blocked state where no data is received.
The client also creates a Datagramsocket object and directs the listening port. Next, create an InetAddress object that resembles the sending address of a network (Inetaddressserveraddress=inetaddress.getbyname ("172.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. * (stringstr= "Hello"; Bytedata[]=str.getbyte ();D atagrampacketpacket=new datagrampacket (Data,data.length, serveraddress,4567); socket.send (packet);)
How Android communicates with the server