The difference between the Java-TCP protocol and the UDP protocol, and the socket.

Source: Internet
Author: User

Socket is used when learning Java, but I still don't understand what is going on. There is no foundation for computer networks. I found some information everywhere. A rough summary of the useful information is provided to facilitate a deeper understanding in the future. For more information, see.

I. Differences between the Tup protocol and the UDP protocol

TCP-the transmission control protocol provides connection-oriented and reliable byte stream services. Before the customer and the server exchange data with each other, a TCP connection must be established between the two parties before data can be transmitted. TCP provides timeout resend, discard duplicate data, test data, traffic control, and other functions to ensure data can be transferred from one end to the other.

UDP-the User Datagram Protocol is a simple datagram-oriented transport layer protocol. UDP does not provide reliability. It only sends the data from the application to the IP layer, but it cannot guarantee that the data can reach the destination. Because UDP does not need to establish a connection between the client and the server before transmitting the datagram, and there is no timeout and re-transmission mechanism, the transmission speed is very fast.

Both TCP and UDP are on the transport layer. To put it simply, when UDP sends data, no matter whether the data reaches the destination or not, the transmission speed is faster. However, data may also be lost. TCP, we know that three handshakes are established and four handshakes are released, so the transmission is more accurate, but the speed may be relatively slow.

To ensure that data is correctly received, TCP requires a confirmation (ACK) when the target computer successfully receives the data ). If the corresponding Ack is not received within a certain time limit, the data packet will be re-transmitted. If the network is congested, this re-transmission will lead to repeated data packets. However, the receiving computer can use the serial number of the data packet to determine whether it is a repeated data packet, and discard it if necessary (this reminds me of the same transmission of reliable and unreliable signals in Linux IPC ).

Ii. Socket

A socket is a basic operation unit that supports TCP/IP network communication. It can be seen as the end point of two-way communication between processes of different hosts, in short, it is a communication convention between the two parties. related functions in the socket are used to complete the communication process.

The following are three socket types of common TCP/IP protocols.

 

Stream socket (sock_stream ):

 

Stream sockets are used to provide connection-oriented and reliable data transmission services. This Service ensures that data can be sent without errors or duplicates and received in order. The reason why stream sockets can implement reliable data services is that they use the Transmission Control Protocol (TCP.

 

Datagram socket (sock_dgram): A datagram socket provides a connectionless service. This service does not guarantee the reliability of data transmission. data may be lost or duplicated during transmission, and data may not be received sequentially. The datagram socket uses the user data protocol for data transmission. Because the data PACKET socket cannot guarantee the reliability of data transmission, it is necessary to handle possible data loss in the program.

 

Original socket (sock_raw): the difference between the original socket and standard socket (standard socket refers to the stream socket and datagram socket described earlier) is: the original socket can read and write IP data packets not processed by the kernel, while the stream socket can only read TCP data, and the data PACKET socket can only read UDP data. Therefore, if you want to access other protocols to send data, you must use the original socket.

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.