浏览器和服务器每进行一次HTTP操作,就建立一次连接,但任务结束就中断连接。如果客户端浏览器访问的某个HTML或其他类型的Web页中包含有其他的Web资源,如JavaScript文件、图像文件、CSS文件等;当浏览器每遇到这样一个Web资源,就会建立一个http会话。
如果浏览器或者服务器在其头信息加入了这行代码 Connection:keep-alive TCP连接在发送后将仍然保持打开状态,于是,浏览器可以继续通过相同的连接发送请求。保持连接节省了为每个请求建立新连接所需的时间,还节约了带宽。在header中加入 --Connection:keep-alive
实现长连接要客户端和服务端都支持长连接。如果web服务器端看到这里的值为“Keep-Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显著地减少下载所需要的时间。要实现这一点, web服务器需要在返回给客户端HTTP头信息中发送一个Content-Length(返回信息正文的长度)头,最简单的实现方法是:先把内容写入ByteArrayOutputStream,然后再正式写出内容之前计算它的大小 无论浏览器 还是 Web 服务器具有较低的 KeepAlive 值,它都将是限制因素。例如,如果客户端的超时值是两分钟,而 Web 服务器的超时值是一分钟,则最大超时值是一分钟。客户端或服务器都可以是限制因素。
HTTP1.1和HTTP1.0相比较而言,最大的区别就是增加了持久连接支持(貌似最新的 http1.0 可以显示的指定 keep-alive), 但还是无状态的,或者说是不可以信任的。 以下是协议描述: 写道
Http Keep-Alive seems to be massively misunderstood. Here's a short description of how it works, under both 1.0 and 1.1
HTTP/1.0 Under HTTP 1.0, there is no official specification for how keepalive operates. It was, in essence, tacked on to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request: Connection: Keep-Alive Then, when the server receives this request and generates a response, it also adds a header to the response: Connection: Keep-Alive Following this, the connection is NOT dropped, but is instead kept open. When the client sends another request, it uses the same connection. This will continue until either the client or the server decides that the conversation is over, and one of them drops the connection. HTTP/1.1 Under HTTP 1.1, the official keepalive method is different. All connections are kept alive, unless stated otherwise with the following header: Connection: close The Connection: Keep-Alive header no longer has any meaning because of this. Additionally, an optional Keep-Alive: header is described, but is so underspecified as to be meaningless. Avoid it. Not reliable HTTP is a stateless protocol - this means that every request is independent of every other. Keep alive doesn’t change that. Additionally, there is no guarantee that the client or the server will keep the connection open. Even in 1.1, all that is promised is that you will probably get a notice that the connection is being closed. So keepalive is something you should not write your application to rely upon. KeepAlive and POST The HTTP 1.1 spec states that following the body of a POST, there are to be no additional characters. It also states that "certain" browsers may not follow this spec, putting a CRLF after the body of the POST. Mmm-hmm. As near as I can tell, most browsers follow a POSTed body with a CRLF. There are two ways of dealing with this: Disallow keepalive in the context of a POST request, or ignore CRLF on a line by itself. Most servers deal with this in the latter way, but there's no way to know how a server will handle it without testing.
Java应用 client用apache的commons-httpclient来执行method 。
常用的apache、resin、tomcat等都有相关的配置是否支持keep-alive。
tomcat中可以设置:
写道
The maximum number of HTTP requests which can be pipelined until the connection is closed by the server. Setting this attribute to 1 will disable HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 will allow an unlimited amount of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.
解释1 长连接指建立SOCKET连接后不管是否使用都保持连接,但安全性较差,
解释2 长连接就是指在基于tcp的通讯中,一直保持连接,不管当前是否发送或者接收数据。
解释3 长连接和短连接这个概念好像只有移动的CMPP协议中提到了,其他的地方没有看到过。
解释4 短连接:比如http的,只是连接、请求、关闭,过程时间较短,服务器若是一段时间内没有收到请求即可关闭连接。
参考:http://blog.csdn.net/shine0181/article/details/7799754,有改动。 |
- 浏览: 78483 次
- 性别:
- 来自: 合肥
最新评论
-
duanhengtao03:
共享目录的的确是个很好的解决办法,但是在文件并发和文件较大的情 ...
多机集群对上载文件的处理方式小结 -
rmzdb:
giianhui 写道同在做eas bos二次开发,希望早日看 ...
从金蝶BOS开发中感悟java的魅力 -
giianhui:
同在做eas bos二次开发,希望早日看到你的文章
从金蝶BOS开发中感悟java的魅力
相关推荐
Java实现Socket长连接和短连接,实现原理可参见个人博客
TCP长连接与短连接示意图
6.2 短连接 通信双方以客户-服务器方式建立TCP连接,应答与请求在同一个连接中完成。系统采用客户/服务器模式,操作以客户端驱动方式发起连接请求,完成一次操作后关闭此连接。 网关与SP之间、网关之间的消息发送后...
主要介绍了HTTP长连接与短连接使用方法及测试,需要的朋友可以参考下
MINA入门实例,实现长连接,短连接通讯。
长连接短连接介绍 1.长连接 Client方与Server方先建立通讯连接,连接建立后不断开,然后再进行报文发送和接收。 2.短连接 Client方与Server每进行一次报文收发交易时才进行通讯连接,交易完毕后立即断开连接。此种...
Mina长连接短连接实例包含Minaclient工程和MinaHost工程,另外还有几个文档,相信对理解这两个工程有很大的帮助
基于Apache Mina实现的TCP长连接和短连接实例.doc
是一个socket长连接和短连接框架其中包括多线程,线程池的创立,由于jar包太大所以没拷上来,可以作为学习应用.
Socket通讯,长连接短链接,包分 包头、指令、内容 包头一般2-4个字节 表示包的长度,指令2个字节 表示做什么操作,内容是具体数据
服务器与客户端交互的连接类型的一种分类和说明以及比较。
TCP/IP通信程序设计的丰富多样性 刚接触TCP/IP通信设计的人根据范例可以很快编出一个通信程 序,据此一些人可能会认为TCP/IP编程很简单。其实不然, TCP/IP编程具有较为丰富的内容。其编程的丰富性主要体现在通信...
线程、线程池、TCP协议长连接短连接的基本入门知识 适合入门
NULL 博文链接:https://bert82503.iteye.com/blog/2147899
android 中长连接变短链接的几种方式集合,希望对大家有所帮助。。。
首先研究下长连接和短连接的性能区别。curl内部是通过socket去连接通讯。socket每次连接最为耗时,如果能够复用连接,长时间连接,减少每次socket连接的时间,则可以大大减少时间,提高效率。 (2) 多线程。单个...
基于ASIO封装的网络库,支持TCP/UDP,支持长连接、短连接,支持组播、单播、广播
如果我们使用了nginx去作为反向代理或者负载均衡,从客户端过来的长连接请求就会被转换成短连接发送给服务器端。 为了支持长连接,我们需要在nginx服务器上做一些配置。 ·【要求】 使用nginx时,想要做到长连接,...
C++ 实现 TCP长,短连接简单Demo,基于VS2017