Java implementation socket long connection and short connection __java

Source: Internet
Author: User
1 Concepts

Socket:socket is actually an encapsulation of TCP/IP, and we can use socket sockets to transmit through the socket. One of the first concepts we need to understand is the channel, which is simply a channel where two pairs of ports can transmit data at any time. What I often say is the so-called establishment of socket connection, that is, the establishment of the client and server-side channel.

Length connection: It is obvious that the long connection is the socket connection has been maintained, that is, the channel has been maintained unobstructed, two to the end can send and receive data at any time, the short connection is that we send one or a limited number of times, the socket channel is closed. First of all, we must understand that after the socket is connected, if none of the side is closed, the channel is maintained, in other words, if either side does not close the connection, this socket connection is a long connection, So the socket in Java itself is to support long connections (such as a simple experiment: server-side does not shut down the connection, server-side every 10 seconds to send data, server-side every time can correctly accept data, this experiment can prove).

So since the socket itself supports long connections, then why do we have to mention the concept of a short connection. Imagine a China Mobile's short message gateway (that is, through the release of Socket communication interface) every minute there are n multiple connections send SMS requests, join the server without any restrictions directly and client use long connection so imagine how much pressure the server needs to bear. So the general socket server side is set to timeout, that is, timeout, if more than timeout server did not receive any data, then the server will shut down the connection, so that the server resources are effectively used. 2 How to achieve the length of connection

In 1, we have introduced the concept of long and short connections, the server if more than timeout time to receive the client's communication will be disconnected, if the client in the timeout time before the second (or less time) to send an activation data to the server side of the timer, So repeated to ensure that the server has been unable to enter the timeout time, so that the connection is maintained, this is the principle of long connection implementation. Here we pass a picture to illustrate:


As the above figure shows, whether a long connection depends entirely on whether the client will send a heartbeat message at timeout time, the length of connection is related to the client, and the server side is no different (except that the server side needs to set timeout).

Code implementations can be referenced: http://download.csdn.net/detail/feichenwangyalin/7951457

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.