TCP and UDP transmission principles of Socket communication in Android

Source: Internet
Author: User

TCP and UDP transmission principles of Socket communication in Android

1. Socket Communication

Android can communicate with the server in two ways: Http and Socket. The biggest difference between the two is that the http connection uses the "request-response method", that is, a connection channel is established during the request. After the client sends a request to the server, the server can return data to the client.

In Socket communication, TCP/IP-based communication means that data can be transmitted directly after both parties establish a connection, and information can be actively pushed during connection, the client does not need to send a request to the server every time. UDP provides a connectionless datagram service. Before sending a datagram, UDP does not need to establish a connection. Instead, it can send data packets without checking the datagram .. So what is socket? Socket, also known as Socket, provides a port to communicate with the outside world in the program, that is, port communication. By establishing a socket connection, data transmission between both parties can be provided.

 

1.1 What is Socket
It is an abstraction layer through which an application sends and receives data. Using Socket, an application can be added to the network to communicate with other applications in the same network. To put it simply, Socket provides a port for communication between the program and the outside, and a data transmission channel for both parties.


1.2Socket Classification
Based on different underlying protocols, Socket implementations are diversified. This article only describes the communication between TCP/IP and UDP. The main Socket types in this protocol family are streamsocket and datagram ramsocket ). Stream socket uses TCP as its peer protocol and provides a reliable byte stream service. The datagram socket uses the UDP protocol to provide the data packaging and sending service. Next, let's take a look at the basic implementation models of these two Socket types.

 

 

Ii. Basic Socket Communication Model


III. Basic Socket implementation principle

 

3.1 TCP-based Socket
1. The server first declares a ServerSocket object and specifies the port number, and then calls the Serversocket accept () method to receive client data. The accept () method is blocked when no data is received. (Socketsocket = serversocket. accept (). Once the received data is received, the received data is read through inputstream.
2. Create a Socket object on the client and specify the IP address and port number of the target host (server) (Socketsocket = newSocket ("172.1610.108", 8080 );), then obtain the output stream (OutputStreamoutputstream = socket. getOutputStream (), and write the data to be sent to outputstream for TCP socket data transmission.
3.2 UDP-based Data Transmission
1. The server first creates an initramsocket object and instructs the listening port. Next, create an empty DatagramPacket data packet object and specify the size for receiving data (byte [] data = new byte [1024]; DatagramPacket packet = new DatagramPacket (data, data. length), use the receive method of the DatagramSocket to receive data sent by the client (datagramSocket. receive (packet), receive () is similar to the accepet () of serversocket. It is blocked when no data is received.
2. The client also creates a initramsocket object and instructs the listening port. Next, create an InetAddress object, which is a network address (InetAddress serveraddress = InetAddress. getByName ("172.161.100 ")). define a string to be sent, create a mongorampacke packet t object, specify the address and port number of the packet sent to the network, and use the send () send data packets. (String str = "hello"; byte [] data = str. getByte (); DatagramPacket packet = new DatagramPacket (data, data. length, server address, 4567); socket. send (packet );)

 

Now let's see how the code is implemented!

4. Implement TCP/IP-based communication for android

1. Android client code:

 


 

2. server code:

 

 

 

Paste the result again:

Server display:

192.168.1.102
192.168.1.102
HelloKitty, Java, hahaha

 

[Summary] Programming Based on the Socket stream network, that is, using the TCP/IP protocol, you can perform operations on file transmission.

5. Implement UDP-based android Communication

1. Android client code:

 


 

2. server code:

 


 

Client image:

Server display:

192.168.1.102 sent: HelloKitty, Java, hahaha, this is udp

[Note] udp multicast has not yet been implemented. I will add it after implementation.

 

Now, the basic Socket network programming is implemented. Note: You need to add permissions when using the android client:

 

     
         
      
          
       
           
        
            
         
      
     
    
   
  
 

Conclusion: UDP-based android and server-side reception shows that communication between the client and the server is normal as long as the port number and the IP address of the target host are correct, in addition, UDP communication between them does not need to be connected, because no matter whether the server enables the preparation to receive data packets, it just sends the data packets. TCP is different. If the server is not enabled, an error is reported, prompting that no connection is established with the server. TCP uses stream-based transmission, and UDP sends packets.

 

 

Related Article

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.