TCP and UDP are important in network transmission, as well as in Android development.
First look at what TCP and UDP are.
What is TCP?
Tcp:transmission Control Protocol Protocol TCP is a connection-oriented, reliable, byte-stream-based transport layer (Transport layer) communication protocol, which is provided by the IETF RFC 793 description (specified). In the simplified computer network OSI model, it accomplishes the functions specified by the fourth layer of the transport layer. The application layer sends a 8-byte data stream for inter-network transmission to the TCP layer, and then TCP splits the data stream into a segment of the appropriate length (the limit of the maximum Transmission Unit (MTU) of the data link layer of the network that is usually connected to the computer). TCP then passes the result packet to the IP layer, which transmits the packet over the network to the TCP layer of the receiving side entity. TCP in order to ensure that no packet loss occurs, give each byte a sequence number, while the serial number also guarantees the delivery to the receiving entity of the packet received sequentially. The receiving entity then sends a corresponding acknowledgment (ACK) to the successfully received byte, and if the sending entity does not receive a confirmation within a reasonable round trip delay (RTT), then the corresponding data (assuming it is lost) will be re-transmitted. TCP uses a checksum function to verify the data for errors, and to calculate checksums when sending and receiving.
First, after the TCP establishes the connection, both sides of the communication can carry on the data transmission simultaneously, secondly, he is full duplex, in the guarantee reliability, uses the time-out retransmission and the piggyback confirmation mechanism.
In the flow control, using the Sliding window Protocol [1], the Protocol stipulates that the window within the unacknowledged group needs to be re-transmitted.
In the congestion control, the slow-start algorithm is adopted.
What is UDP?
UDP is the abbreviation of User Datagram protocol, the Chinese name is the Subscriber packet protocol, is a connectionless transport layer protocol in the OSI Reference Model, and provides a simple and unreliable information transfer service for transaction. It is the official specification of the IETF RFC 768 that is UDP. It is used in the network as the TCP protocol to process packets. In the OSI model, the fourth layer, the transport layer, is in the upper layer of the IP protocol. UDP has the disadvantage of not providing datagram grouping, assembling, and not sorting packets, that is, when the message is sent, it is not possible to know whether or not it arrives safely and completely. UDP is used to support network applications that need to transfer data between computers. A large number of client/server mode network applications, including the network video conferencing system, require the use of UDP protocol. UDP protocol has been used for many years since its inception, although its original glory has been obscured by some similar agreements, but even today, UDP is still a very practical and feasible network Transport layer protocol.
As with the well-known TCP (Transmission Control Protocol) Protocol, the UDP protocol is located directly on the top level of the IP (Internet Protocol) protocol. Depending on the OSI (Open Systems Interconnect) Reference Model, both UDP and TCP are transport-layer 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. The first 8 bytes of each datagram are used to contain the header information, and the remaining bytes are used to contain the specific transmitted data.
The use of TCP and UDP in Android is exactly the same as in Java.
First we look at the TCP connection, one for the TCP connection:
There is not much to say, look directly at the code!
TCP Server-side code:
[Java]View Plaincopy
- try {
- Boolean Endflag = false;
- ServerSocket ss = New ServerSocket (12345);
- While (!endflag) {
- //Waiting for client to connect
- Socket s = ss.accept ();
- BufferedReader input = new BufferedReader (Newinputstreamreader (S.getinputstream ()));
- //Note the second parameter data is true will automatically flush, otherwise need to manually operate Output.flush ()
- PrintWriter output = Newprintwriter (S.getoutputstream (),true);
- String message = Input.readline ();
- LOG.D ("TCP Demo", "message from Client:" +message);
- OUTPUT.PRINTLN ("message received!");
- //output.flush ();
- if ("ShutDown". Equals (Message)) {
- Endflag=true;
- }
- S.close ();
- }
- Ss.close ();
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- TCP Client code:
- ?
- try {
- Socket s = new socket ("localhost", 12345);
- //Outgoing stream redirect to socket
- OutputStream out = S.getoutputstream ();
- //Note the second parameter data is true will automatically flush, otherwise need to manually operate Out.flush ()
- PrintWriter output = new PrintWriter (out, true);
- Output.println ("Hello ideasandroid!");
- BufferedReader input = new BufferedReader (Newinputstreamreader (S
- . getInputStream ()));
- //Read line (s)
- String message = Input.readline ();
- LOG.D ("TCP Demo", "message from Server:" + message);
- S.close ();
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
Below we look at UDP:
UDP server-side code:
[Java]View Plaincopy
- Ports that the UDP server listens on
- Integer port = 12345;
- //Received byte size, the data sent by the client cannot exceed this size
- byte[] message = new byte[1024];
- try {
- //Establish socket connection
- Datagramsocket datagramsocket = new Datagramsocket (port);
- Datagrampacket datagrampacket = new Datagrampacket (Message,
- Message.length);
- try {
- While (true) {
- //Prepare to receive data
- Datagramsocket.receive (Datagrampacket);
- LOG.D ("UDP Demo", datagrampacket.getaddress ()
- . Gethostaddress (). ToString ()
- + ":" + new String (Datagrampacket.getdata ()));
- }
- } catch (IOException e) {
- E.printstacktrace ();
- }
- } catch (SocketException e) {
- E.printstacktrace ();
- }
UDP Client code:
[Java]View Plaincopy
- Public static void Send (String message) {
- Message = (Message = = null?) "Hello ideasandroid!": message);
- int server_port = 12345;
- Datagramsocket s = null;
- try {
- s = new Datagramsocket ();
- } catch (SocketException e) {
- E.printstacktrace ();
- }
- InetAddress local = null;
- try {
- //Swap server-side IP
- Local = inetaddress.getbyname ("localhost");
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- }
- int msg_length = Message.length ();
- byte[] Messagemessagebyte = Message.getbytes ();
- Datagrampacket p = new Datagrampacket (Messagebyte, Msg_length, Local,
- Server_port);
- try {
- S.send (P);
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Android Development: How to implement TCP and UDP transport