Network Applications (ii)----How Android uses TCP and UDP for communication, socket programming

Source: Internet
Author: User

TCP Transmission Control Protocol, is a connection-oriented, reliable, byte-stream-based transport layer communication protocols.

UDP User Datagram Protocol, which is a non-connected Transport layer protocol in TCP/IP model, provides a simple and unreliable information delivery service oriented to transaction.

Socket commonly known as socket, Chinese means socket. A server may provide a number of services, each of which corresponds to a socket (it can also be said that each socket is a socket, if the customer needs any kind of service, plug into the corresponding socket). And the customer's plug is also a socket. It is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol and is a set of interfaces.

Basic socket operations include:

Connect the remote server, send data, receive data, close the connection, bind the port, listen to the data, and accept the connection from the remote machine on the bound port.

---------------------------------------------------------------------------

The server communicates with the client, both of which instantiate a socket. However, the server and client socket is not the same, the client can be connected to remote server, send data, receive data, close the connection and so on. The server also needs to implement the bound port, listen to the data, and accept connections from the remote machine on the bound port.

Android provides two classes in java.net: ServerSocket and sockets, which are used to instantiate the server socket, which is used to instantiate the client socket. After a successful connection, both ends of the application produce a socket instance that operates on the instance and completes the client-to-server session.

--------------------------------------------

Socket client=new socket ("192.168.1.23", 2012);//The first parameter is the IP address of the destination server, and 2012 is the port number of the destination server

Note: 0~1023 is reserved for the system, the user should set the port number should be greater than 1023

-------------------------------------------

Socket class Important methods:

Public InputStream getInputStream () read out the data stream in the socket

Public OutputStream Getoutputstream () writes data to the socket

Public synchronized void Close () to close the socket

---------------------------------------------------------------

ServerSocket socketserver=new ServerSocket (2012);//2012 indicates the number the server is listening to

After ServerSocket is constructed, the Serversocket.accept () method needs to be called to wait for the client's request (because the socket is bound to the port, so it knows which client is requesting it). The Accept () method returns the socket instance of the client requesting the service item, and then operates the transmitted information through the method of the returned socket instance. When the socket object has finished working, use the close () method to close it.

Important methods of the ServerSocket class:

Public socket accept () waits for the socket request, the direct connection is opened, and the method returns a newly opened connection to the socket object.

public void Close () to close the server socket

-------------------------------------------

There are generally two types of sockets: TCP Sockets and UDP sockets

--------------------------------------------------------------

Using TCP Communication

After TCP establishes the connection, both sides of the communication can carry on the data transmission simultaneously, in the guarantee reliability, uses the time-out retransmission and the pass-through confirmation mechanism; In the flow control, adopts the sliding window protocol, the Protocol stipulates that the non-confirmed group in the window needs retransmission; In the congestion mechanism, the slow-start algorithm is adopted.

The main steps for TCP server-side work are as follows.

Step 1 calls serversocket (int port) to create a serversocket and bind to the specified port.

Step 2 calls Accept (), listens for connection requests, and if the client requests a connection, accepts the connection and returns the communication socket.

Step 3 calls the Getoutputstream () and getInputStream () of the socket class to get the output and input streams to begin sending and receiving network data.

Step 4 Close the communication socket.

The main steps for TCP client work are as follows.

Step 1 Call the socket () to create a stream socket and connect to the server side.

Step 2 calls the Getoutputstream () and getInputStream () methods of the socket class to get the output and input streams to begin sending and receiving network data.

Step 3 Close the communication socket.

------------------------------------------------------------------

Using UDP communication

UDP has the disadvantage of not providing datagram grouping, assembling, and not ordering packets, that is, when the message is sent, it is not possible to know whether it arrives safely and completely. UDP is used to support network applications that require the transfer of data between computers, and many client/server models, including network video conferencing systems, require UDP protocols. The main function of the UDP protocol is to compress the network data traffic into the form of the datagram. A typical datagram is a transmission unit of binary data. UDP transmission principle 2-6 is shown.

The main steps for UDP server-side work are as follows.

Step 1 calls datagramsocket (int port) to create a datagram socket and bind to the specified port.

Step 2 calls Datagrampacket (Byte[]buf,int length) and establishes a byte array to receive the UDP packets.

Step 3 calls the receive () of the Datagramsocket class to accept the UDP packet.

Step 4 Close the datagram socket.

The main steps for UDP client work are as follows.

Step 1 Call Datagramsocket () to create a packet socket.

Step 2 calls Datagrampacket (Byte[]buf,int offset,int length,inetaddress address,int Port) to establish the UDP packet to send.

Step 3 calls the Send () of the Datagramsocket class and sends the UDP packet.

Step 4 Close the datagram socket.

Network Applications (ii)----How Android uses TCP and UDP for communication, socket programming

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.