Java socket Programming (i) socket transfer Mode _java programming

Source: Internet
Author: User
Tags require
Article Source: ASPCN Author: Sun Wen

Socket transfer Mode

Sockets has two main modes of operation: connection-oriented and connectionless. Connection-oriented sockets operations are like a phone call, They have to establish a connection and a call to one person. All things arrive in the same order as they set out. A connectionless sockets operation is like a mail delivery, and there is no guarantee that multiple messages may arrive in the same order as they were when they started.

Which model to use is the need of the mail application. If reliability is more important, it's better to use a connection-oriented operation. For example, file servers require the correctness and order of their data. If some data is lost, the effectiveness of the system will be lost. Some servers, For example, send some chunks of data intermittently. If the data is lost, the server does not want to send it again. Because when the data arrives, it may be obsolete. Ensuring that the data is ordered and correct requires additional operational memory consumption, and additional costs will reduce the system's response rate.

Connectionless operations use datagram protocols. A datagram is a self-contained unit that contains all of the information for this post. Think of it as an envelope, it has the destination address and the content to send. The socket in this mode does not need to connect a socket for a purpose, It simply throws out the datagram. Connectionless operations are fast and efficient, but data security is poor.

Connection-oriented operations use the TCP protocol. A socket in this mode must obtain a connection to the socket of the destination before sending the data. Once the connection is established, sockets can use a stream interface: Open-read-write-close. All messages sent are received in the same order at the other end. Connection-oriented operations are less efficient than connectionless operations, but the data is more secure.

Sun has always been a supporter of network building, so it is not surprising to support sockets in Java. In fact, Java reduces the difficulty of building a sockets program. Each transport mode is encapsulated into a different class. The class for the connection will be discussed first.

Sockets has two main modes of operation: connection-oriented and connectionless. Connection-oriented sockets operations are like a phone call, They have to establish a connection and a call to one person. All things arrive in the same order as they set out. A connectionless sockets operation is like a mail delivery, and there is no guarantee that multiple messages may arrive in the same order as they were when they started.

Which model to use is the need of the mail application. If reliability is more important, it's better to use a connection-oriented operation. For example, file servers require the correctness and order of their data. If some data is lost, the effectiveness of the system will be lost. Some servers, For example, send some chunks of data intermittently. If the data is lost, the server does not want to send it again. Because when the data arrives, it may be obsolete. Ensuring that the data is ordered and correct requires additional operational memory consumption, and additional costs will reduce the system's response rate.

Connectionless operations use datagram protocols. A datagram is a self-contained unit that contains all of the information for this post. Think of it as an envelope, it has the destination address and the content to send. The socket in this mode does not need to connect a socket for a purpose, It simply throws out the datagram. Connectionless operations are fast and efficient, but data security is poor.

Connection-oriented operations use the TCP protocol. A socket in this mode must obtain a connection to the socket of the destination before sending the data. Once the connection is established, sockets can use a stream interface: Open-read-write-close. All messages sent are received in the same order at the other end. Connection-oriented operations are less efficient than connectionless operations, but the data is more secure.

Sun has always been a supporter of network building, so it is not surprising to support sockets in Java. In fact, Java reduces the difficulty of building a sockets program. Each transport mode is encapsulated into a different class. The class for the connection will be discussed first.

Related Article

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.