Multi-task processing: Control default actions

Source: Internet
Author: User
Developers who control the default behavior TCP/IP protocol spend a lot of time considering the default behavior of the Protocol to meet the needs of most applications. (If you are skeptical about this, you can refer to RFC1122 and 1123. based on years of experience, they have made a detailed description of the Recommendation behavior of TCP/IP protocol implementation... syntaxHighlighter. all (); the developer who controls the default behavior TCP/IP protocol takes a lot of time to consider the default behavior of the Protocol to meet the needs of most applications. (If you are skeptical about this, refer to RFC1122 and 1123. based on years of experience, they provide a detailed description of the recommended behavior for implementing TCP/IP protocols .) These designs are ideal for most applications, but "a size that fits all needs" is very rare for all scenarios. The preceding example shows the UDP echo client. By default, the receive () method of the DatagramSocket class will block waiting for a data packet indefinitely. In our example, by specifying a receiving timeout value on a UDP socket and using setSoTimeout () in the TimeLimitEchoProtocol class, the default Protocol behavior is changed. 1 Keep-Alive if there is no data exchange within a period of time, each terminal of the communication may doubt whether the other party is still active. TCP provides a keep-alive mechanism, which sends a probe message to another terminal after a period of inactivity. If the other terminal is still active, it will return a confirmation message. If you still do not receive the confirmation message from another terminal after several attempts, terminate the probe message, close the socket, and throw an exception during the next I/O operation. Note that the application can detect the keep-alive mechanism only when the detection information fails. Socket: keep active boolean getKeepAlive () void setKeepAlive (boolean on) by default, the keep-alive mechanism is disabled. Call setKeepAlive () to set it to true to enable the keep-alive mechanism. 2. Once a Socket or initramsocket instance is created for the size of the sending and receiving cache, the operating system must allocate a cache for it to store the received and sent data. (We will introduce it in more detail in section 6.1) Socket, DatagramSocket: Set and obtain the size of the sending and receiving cache int getReceiveBufferSize () void setReceiveBufferSize (int size) int getSendBufferSize () void setSendBufferSize (int size) getReceiveBufferSize (), setReceiveBufferSize (), getSendBufferSize (), and setSendBufferSize () the method is used to obtain and set the size of the receiving and sending buffer (in bytes ). It should be noted that the size specified here is only provided as a suggestion, and the actual size may be different from it. You can also specify the size of the receiving buffer on ServerSocket. However, this actually sets the size of the receiving buffer for the new Socket instance created by the accept () method. Why can we only set the size of the receiving buffer instead of the size of the sending buffer? When a new Socket is received, it can immediately start to receive data. Therefore, you need to set the buffer size before the accept () method completes the connection. On the other hand, you can control when data is sent on the new socket, so there is still time to set the size of the sending buffer before sending. ServerSocket: sets/gets the size of the received buffer of the socket. int getReceiveBufferSize () void setReceiveBufferSize (int size) getReceiveBufferSize () and setReceiveBufferSize () the size (in bytes) of the receiving buffer of the Socket instance created by the method ). 3 timeout as described earlier, many I/O operations will block wait if they cannot be completed immediately: Read operations will block wait until at least one byte is readable; the receiving operation will block the wait until the connection is established successfully. Unfortunately, there is no blocking time limit. You can specify a maximum blocking time for various operations. Socket, ServerSocket, and DatagramSocket: Set/get I/O timeout int getSoTimeout () void setSoTimeout (int timeout) getSoTimeout () and setSoTimeout () the methods are used to obtain and set the maximum blocking time for read/receive data operations and accept Operations respectively. The value 0 indicates that the Operation Never times out. If the blocking duration exceeds the timeout duration, an exception is thrown. 4. address reuse in some cases, you may want to bind multiple sockets to the same socket address. For UDP multicast, multiple applications can be added to the same multicast group on the same host. For TCP, when a connection is closed, one end (or both ends) of the communication must Wait for a period of Time in the "Time-Wait" status, to clear lost data packets during transmission (see Section 6.4.2 ). Unfortunately, the communication terminal may not be able to Wait until Time-Wait ends. In both cases, you must be able to bind to the address in use, which requires address reuse. Socket, ServerSocket, and DatagramSocket: Set/get address reuse boolean getReuseAddress () void setReuseAddress (boolean on) getReuseAddress () and setReuseAddress () methods are used to obtain and set the address reuse license. If it is set to true, address reuse is enabled. 5. Eliminate the buffer delay TCP caches data until enough data is sent once to avoid wasting network resources by sending too small packets. Although this function is beneficial to the network, applications may be unable to tolerate the resulting buffer latency. Fortunately, the cache function can be manually disabled. Socket: Set/obtain TCP buffer latency boolean getTcpNoDelay () void setTcpNoDelay (boolean on) getTcpNoDelay () and setTcpNoDelay () methods are used to obtain and set whether to eliminate buffer latency. Setting the value to true indicates disabling the buffer delay function. 6. Urgent Data assume that you have sent a lot of data to a slow receiver, and suddenly you have other data that you need urgently. If the data is sent to the output stream, it will be appended to the regular data queue, which cannot be ensured that the receiver can receive the data immediately. To solve this problem, the TCP protocol contains the urgent Data concept, which can (theoretically) Jump to the front. Because they can bypass conventional data streams, these data are called out-of-channel data. Socket: emergency data void sendUrgentData (int data) boolean getOOBInline () void setOOBInline (boolean on) the sendUrgentData () method must be called to send emergency data, it sends the byte of its int parameter. To receive this byte, you must pass the true parameter for the setOOBInline () method to enable the receiver to receive data outside the channel. This byte is received in the input stream of the receiver. The data sent before the emergency byte is placed before the emergency byte in the receiver's input stream. If the recipient's function of receiving data outside the channel is not enabled, the emergency byte will be silently discarded. Note that the Urgent Data in Java is almost useless, because the urgent bytes are mixed with the regular bytes in the transmission order. In fact, Java receivers cannot tell whether they are receiving urgent data. 7. When the close () method of the socket is called, it returns immediately even if no data is sent in the buffer zone of the socket. In this case, the host will fail at a certain time. In fact, you can choose to let the close () method "Stay" or block for a period of time until all data has been sent and confirmed, or timeout occurs. For details, see Section 6.4.2 Socket: Stop int getSoLinger () void setSoLinger (boolean on, int linger) in the close () method. If setSoLinger () is called and set to true, the close () method that will be called later will block the wait until the remote terminal returns confirmation information for all the data, or the specified Timeout (seconds) occurs ). If a timeout occurs, the TCP connection is forcibly closed. If the stop function is enabled, the getSoLinger () method returns the specified timeout value; otherwise,-1 is returned. 8. Broadcast licenses are requested explicitly by some operating systems. You can control broadcast licenses. As mentioned earlier, the DatagramSockets class provides the broadcast service function. DatagramSocket: sets/obtains broadcast licenses. boolean getBroadcast () void setBroadcast (boolean on) getBroadcast () and setBroadcast () are used to obtain and set broadcast licenses respectively. If it is set to true, broadcast is allowed. In Java, broadcast is allowed by default. 9 some networks of the communication level provide enhanced services or "additional insurance" for packets meeting the service conditions ". The communication level (traffic class) of a data packet is specified by its internal value when the data packet is transmitted over the network. For example, some networks provide a higher priority for data packets of the "Golden service" level to reduce transmission latency and packet loss probability. Some networks may select routes for data packets based on the specified communication level. However, it is important to note that the network provider charges additional fees for such services, so there is no guarantee that these options actually take effect. Socket, DatagramSocket: sets/obtains the communication level int getTrafficClass () void setTrafficClass (int tc). The communication level is usually specified by an integer or a group of bits. The number and meaning of BITs depend on the IP protocol version used. 10 selecting TCP based on performance is not the only protocol available for sockets. What protocols are used depends on what the application focuses on. Java allows developers to give "Suggestions" for specific implementation based on the importance of different performance characteristics to applications ". Based on these suggestions, the underlying network system may choose between different protocols that can provide the same data flow service and have different performance characteristics. Socket, ServerSocket: Specify the Protocol Parameter void setinclucepreferences (int connectionTime, int latency, int bandwidth). The performance parameters of the Socket are represented by three integers, representing the connection time, latency, and bandwidth respectively. The specific value is not important. Java will compare the values of various standard parameters and return the best matching protocol. For example, if connectionTime and latency are both equal to 0 and bandwidth is equal to 1, the protocol that maximizes the bandwidth will be selected. Note: To make this method take effect, it must be called before the socket establishes a connection.
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.