Principles and comparison of TCP and UDP

Source: Internet
Author: User

Basic TCP and UDP knowledge

  TCP (Transmission Control Protocol) and UDP (User datasync protocol) Protocols belong to the transport layer protocol. Among them, TCP provides reliable data transmission in the IP environment. Its services include data stream transmission, reliability, effective traffic control, full-duplex operations and multiplexing. Send data packets through connection-oriented, end-to-end, and reliable data packets. Generally speaking, it opens a connection channel for the data to be sent in advance, and then sends the data. UDP corresponds to applications with low reliability requirements and economic transmission. TCP supports the following application-layer protocols: Telnet, FTP, and SMTP. UDP supports the following application-layer protocols: NFS (Network File System) and SNMP (Simple Network Management Protocol), DNS (Primary Domain Name System), TFTP (Common File Transfer Protocol), etc.

Connection-oriented TCP

"Connection-oriented" means that a connection must be established with the other party before formal communication. For example, if you call someone else, you must wait until the line is connected and the other party picks up the microphone to talk to each other. TCP (Transport Control Protocol) is a connection-based protocol. That is to say, a reliable connection must be established with the other party before sending and receiving data. A TCP connection can be established only after three "conversations". The process is very complex. Here we only provide a simple and visual introduction, as long as we can understand this process. Let's take a look at the simple process of the three conversations: host a sends a connection request packet to host B: "Can I send data to you? ", This is the first conversation; host B sends a packet to host a that agrees to the connection and requires synchronization (synchronization means that two hosts are sending, receiving, and coordinating):" Yes, when are you? ", This is the second conversation. host a sends another packet to confirm the requirements of host B for synchronization:" I will send it now, please proceed! ", This is the third dialogue. The purpose of the three "dialogs" is to synchronize the transmission and receipt of data packets. After three "dialogs" are entered, host a will send data to host B.

Non-connection-oriented UDP

  "Non-connection oriented" means that you do not need to establish a connection with the other party before the formal communication, and send the connection directly regardless of the other Party's status. Similar to text messages: when you send a text message, you only need to enter the recipient's phone number.

User Data Protocol (UDP) is the protocol corresponding to TCP. It is a non-connection-oriented protocol. Instead of establishing a connection with the other Party, it directly sends data packets!

UDP is applicable to application environments that only transmit a small amount of data at a time and have low reliability requirements. For example, the "ping" command is often used to test whether the TCP/IP communication between two hosts is normal. In fact, the "ping" command is used to send UDP packets to the other host, then, the host of the other party confirms that the packet is received. If the message indicating whether the packet arrives is returned in time, the network is connected.

Difference Between TCP and UDP

  

  TCP UDP
Connected? Connection-oriented Non-connection oriented
Transmission reliability Reliable Unreliable
Application scenarios Transmit large amounts of data Small amount of data
Speed Slow Fast

Implement TCP and UDP in Java

In Java APIs, socket is a connection socket, Java provides corresponding classes for TCP and UDP, and TCP is java.net. serversocket (for server) and java.net. socket (for the client); UDP is java.net. datagramsocket. The obvious difference in APIS is that UDP socket programming does not provide the listening function, that is, the two sides are more equal and the interfaces are the same. However, to use UDP to implement the C/S structure, you can use datagramsocket. Receive () to implement functions similar to listening. Because receive () is a blocking function, that is, waiting for data to be received. If no data is received, it enters the waiting state. The program stops at the receive () function. if it receives data, the program will continue to run. Therefore, the next action can be determined based on the read datagram, which achieves the same effect as the network listener.

If UDP is not connected, send () and receive () are sent. TCP needs to obtain an outputstream and inputstream to read and write data. When obtaining a stream, it is equal to establishing a connection.

The initramsocket methods include connect (socketaddress ADDR) and disconnect ().

  • The CONNECT () method does not actually establish a TCP connection. The connection only sends packets to the remote host and remote port specified by initramsocket. In this case, an illegalargumentexception exception is thrown when you try to send packets to another host or port.
  • Disconnect (): Stop the "connection" that has been connected to the initramsocket, so that you can send and receive packets from any host and port again.

Principles and comparison of TCP and UDP

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.