Java Network Programming TCP vs UDP

Source: Internet
Author: User

TCP (Transmission Control Protocol): connection-oriented, unreliable, and data stream service.
UDP (User datainprotocl User Datagram Protocol): For connectionless, unreliable, and datagram services.
TCP transmission is different from UDP transmission. TCP transmission is streaming. A connection must be established first, and the data stream is transmitted along the connected line (virtual circuit. Therefore, TCP data streams are not the same as UDP datagram. Each datagram must contain the destination address and port, because each datagram must be routed separately. For TCP transmission, you only need to specify the target address and port when establishing a connection.
I don't know anything about computer networks that I just learned (the fourth edition of Andrew S. Tanenbaum translated by Pan aimin to Tsinghua University Press. The above is just a brief introduction. If you want to learn the Internet, this book is worth recommending. The English version is computer networks.
in terms of image, TCP is like making a call, and UDP is like generating a report.
just like sending a report, UDP does not depend on the client or server. Both parties are equal.
at the micro level, only one packet is sent to the client and the listener to the server. Sending a datagram to a vro at the sending end is like sending a telegram to the post office. What follows is that the sender cannot control or know it. Therefore, it is not reliable, and there may be loss of packets that are unknown. Just as each telegram has a recipient, each datagram has a destination address and port.
like making a call, each TCP connection is divided into a client and a server. The initiator of the connection (equivalent to the dialing caller) is the client, and the listener (equivalent to the person waiting to answer the call on the phone) is the server. The initiator specifies the server address and port to be connected (equivalent to dialing), and the listener establishes a connection by shaking hands with the initiator three times (equivalent to hearing the phone ring to answer the phone ). After the connection is established, both parties can send and receive data (by phone) to each other ).
in Java, TCP programming is implemented mainly through the serversocket/socket class. Socket. getinputstream ()/getoutputstream () is used to obtain the input/output data stream. Serversocket. Accept () is used to listen for establishing a connection.

this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/nyzhl/archive/2007/07/24/1705039.aspx

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.