How to establish socket long connection???

Source: Internet
Author: User
Tags socket error

Source of Information: http://www.cnblogs.com/lexus/archive/2011/11/15/2249238.html

Http://bbs.chinaunix.net/thread-709365-1-1.html

Http://topic.csdn.net/t/20040810/19/3261335.html

Popular said after the connection is long connection, hehe.
To give you an example, the Internet agent: Proxy
Client <---> Proxy <---> server

Like C to access Sohu's server
P put s all the page content to C, turn off the link with C.
At this point you can choose not to close the P and S links.

When another c also wants to access Sohu, you can use the long link just now.

Long connection is the client and server to establish a socket connection, the connection after use, not immediately shut down, but to maintain this connection, if the next need to communicate with the server, immediately enable the connection for data communication.

Of course, to maintain a long connection, you must check the status of the connection (disconnect).


http long connections and short connections

Http://hi.baidu.com/loleafver/blog/item/a222c9d853d4f1e638012fa0.html Good Article

HTTP is stateless  
that is, the browser and server make a connection once for each HTTP operation, but the connection is interrupted at the end of the task. If the client browser accesses an HTML or other type of Web page that contains other Web resources, such as JavaScript files, image files, CSS files, and so on, when the browser encounters such a Web resource, an HTTP session is created

The biggest difference between HTTP1.1 and HTTP1.0 is the addition of persistent connection support (which appears to be the latest http1.0 the specified keep-alive), but stateless or untrustworthy.

The browser can continue to send requests through the same connection if the browser or server has added this line of code to its header

connection:keep-alive

TCP connection will remain open after it is sent. Keeping the connection saves the time it takes to establish a new connection for each request, and saves bandwidth. The

Implements long connections to support long connections on both the client and server side.

If the Web server sees the value here as "keep-alive", or if you see that the request uses an HTTP 1.1 ( HTTP 1.1 default persistent connection ), it can take advantage of the benefits of a persistent connection. When a page contains multiple elements (such as applets, pictures), it significantly reduces the time it takes to download. To do this, the  web server needs to send a content-length (the length of the message body) back to the client HTTP header information, the simplest way to do this is to write the content to the Bytearrayoutputstream,   calculates its size before it is formally written out

will be a limiting factor, regardless of whether the client browser (Internet Explorer) or the WEB server has a lower KeepAlive value . For example, if the timeout value for the client is two minutes and the Web server timeout is one minute, the maximum timeout is one minute. Either the client or the server can be a constraint

Add--connection:keep-alive  to the header,
Add this in the HTTP protocol request and response to maintain a long connection. &NBSP
to encapsulate the message application of the HTTP message data body is very easy to use

http://blog.csdn.net/thomescai/article/details/6804270

http://blog.csdn.net/xijiaohuangcao/article/details/6105623


to those who make me confused http, TCP, UDP, Socket 2010-12-29 18:28 408 People read comments (1) Collection report

Let's start with a TCP, UDP, and HTTP relationship, and pay attention to the red mark part.

1, TCP/IP is a protocol group, can be divided into three levels: Network layer, Transport layer and application layer.
There are IP protocols, ICMP protocols, ARP protocols, RARP protocols and BOOTP protocols on the network layer.
There are TCP protocols and UDP protocols in the transport layer.
In the application layer are FTP, HTTP, TELNET, SMTP, DNS and other protocols.
Therefore, HTTP itself is a protocol that transmits hypertext from a Web server to a local browser.

2, the HTTP protocol is based on the request/response model. The customer first establishes a TCP link to the server and sends a request to the server, which contains the request method, URI, protocol version, and the associated MIME-style message. The server responds to a status line that contains the protocol version of the message, a success and failure code, and the associated MIME style message.
Http/1.0 creates a new TCP link for each HTTP request/response, so a page containing HTML content and pictures will need to establish multiple short TCP links. The establishment of a TCP link will require 3 handshake.
In addition, in order to obtain the appropriate transmission speed, TCP will need to spend additional loop link time (RTT). Each link is built with this recurring overhead, which does not carry actual useful data, but only guarantees the reliability of the link, so http/1.1 proposes a sustainable link implementation approach. http/1.1 will only establish a TCP link and use it repeatedly to transmit a series of request/response messages, thus reducing the number of links established and the frequent link overhead.

3. Conclusion: Although HTTP itself is a protocol, it is ultimately based on TCP. However, at present, some people are studying the HTTP protocol based on TCP+UDP Hybrid.

What's the socket?
The socket is the intermediate software abstraction layer of the application layer communicating with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, which is the complex TCP/IP protocol family hidden behind the socket interface, for users, a group of simple interface is all, let the socket to organize data to meet the specified protocol.

Http://hi.baidu.com/einsof/blog/item/f2f3a84b5d1a30fb82025ca0.html

What is short connection, long connection, Socket (basic knowledge literacy) 2010-01-05 16:06

Socket

The image description of the socket agreement
The original meaning of the socket is "hole" or "socket". Here as a process communication mechanism for 4BDS Unix, take the latter meaning. The socket is very similar to a telephone socket. Take a national telephone network for example. Telephone calls are equivalent to each other 2 processes of communication, the area code is its network address, a unit in the area of the switch equivalent to a host, the host assigned to each user's internal number is equivalent to the socket number. Any user prior to the call, first to occupy a telephone, equivalent to the application of a socket, and to know the other person's number, equivalent to the other side has a fixed socket. Then dial the call to the other side, which is equivalent to sending a connection request (if the other person is not in the same zone, and dial the other area code, equivalent to the network address). If the other party is present and free (equivalent to another host of communication and can accept the connection request), pick up the phone microphone, the two sides can officially call, the equivalent of successful connection. Both sides of the call process, is a direction of the telephone signal and the other from the phone to receive signals, the equivalent of sending data to the socket and receive data from the socket. After the call is over, one side hangs up the telephone equivalent of closing the socket and undoing the connection.

See Encyclopedia for detailed content.


Usually a short connection is this: connection-> transmission data-> close connection
That what is the long connection. For a general long connection relative to a short connection, a long connection does not close the connection after the transmission is completed, while the continuous sending package remains connected pending processing of the next packet.
such as: Connect-> transmit Data-> Keep connection-> transmit data-> ... -> closes the connection.

When do you use a short connection?
General long connection for a few client-end to server-end frequent communication, such as: Database connection with long connection, if the use of short connection frequent communication will cause socket error, and frequent socket creation is also a waste of resources.
HTTP services like Web sites generally use short links, because long connections consume a certain amount of resources for the server, and the connections of tens of thousands or even billions of clients, such as Web sites, can save some resources. (View source)


TCP/IP Communication interpretation length link

One. Communication mode
There are three major categories:
A Server/client Way
1. A client side connects to a server side, or point-to-point (peer to peer):
2. Multiple client parties connect to a server side, which is also the usual concurrent server approach.
3. A client-side connection to multiple server parties is rare, mainly
For a client to send requests to multiple servers.

(ii) connection mode
1. Long connection
The client and the server first establish a communication connection, and the connection is established and continuously opened.
Then the message is sent and received. In this way because the communication connection has been
exists, you can use the following command to see if the connection is established:
Netstat–f inet|grep Port number (for example, 5678).
This approach is often used for point-to-point communication.

2. Short connection
Client side communicates with server every time a message is sent and received
Then disconnect immediately after the transaction is complete. This approach is often used in a little bit of
Communications, such as multiple client connections to a server.

(iii) Sending and receiving mode
1. Asynchronous
Message sending and receiving are separate, independent, and do not affect each other. Such side
The formula is divided into two types:
(1) Asynchronous duplex: Receive and send in the same program, there are two different

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.