Socket 短连接、长连接_YTmarkit的空间_百度空间

Socket 短连接、长连接_YTmarkit的空间_百度空间

Socket 短连接、长连接

socket

Socket协议的形象描述
socket的 英文原义是“孔”或“插座”。在这里作为4BDS UNIX的进程通信机制,取后一种意思。socket非常类似于电话插座。以一个国家级电话网为例。电话的通话双方相当于相互通信的2个进程,区号是它的 网络地址;区内一个单位的交换机相当于一台主机,主机分配给每个用户的局内号码相当于socket号。任何用户在通话之前,首先要占有一部电话机,相当于 申请一个socket;同时要知道对方的号码,相当于对方有一个固定的socket。然后向对方拨号呼叫,相当于发出连接请求(假如对方不在同一区内,还 要拨对方区号,相当于给出网络地址)。对方假如在场并空闲(相当于通信的另一主机开机且可以接受连接请求),拿起电话话筒,双方就可以正式通话,相当于连 接成功。双方通话的过程,是一方向电话机发出信号和对方从电话机接收信号的过程,相当于向socket发送数据和从socket接收数据。通话结束后,一 方挂起电话机相当于关闭socket,撤消连接。

详细内容见百科


通常短连接是这样:连接->传输数据->关闭连接
那什么是长连接?一般长连接相对短连接而言的,长连接在传输完数后不关闭连接,而不断的发送包保持连接等待处理下一个数据包。
such as: 连接->传输数据->保持连接 -> 传输数据-> 。。。 ->关闭连接。

那什么时候用短连接呢?
一般长连接用于少数client-end to server-end的频繁的通信,例如:数据库的连接用长连接, 如果用短连接频繁的通信会造成socket错误,而且频繁的socket 创建也是对资源的浪费。
而像WEB网站的http服务一般都用短链接,因为长连接对于服务端来说会耗费一定的资源,而像WEB网站这么频繁的成千上万甚至上亿客户端的连接用短连接会更省一些资源。(查看来源)


TCP/IP通信解读长短链接

一。通信方式
主要有以下三大类:
(一)SERVER/CLIENT方式
1.一个Client方连接一个Server方,或称点对点(peer to peer):
2.多个Client方连接一个Server方,这也是通常的并发服务器方式。
3.一个Client方连接多个Server方,这种方式很少见,主要
用于一个客户向多个服务器发送请求情况。

(二)连接方式
1.长连接
Client方与Server方先建立通讯连接,连接建立后不断开,
然后再进行报文发送和接收。这种方式下由于通讯连接一直
存在,可以用下面命令查看连接是否建立:
netstat –f inet|grep 端口号(如5678)。
此种方式常用于点对点通讯。

2.短连接
Client方与Server每进行一次报文收发交易时才进行通讯连
接,交易完毕后立即断开连接。此种方式常用于一点对多点
通讯,比如多个Client连接一个Server.

(三)发送接收方式
1.异步
报文发送和接收是分开的,相互独立的,互不影响。这种方
式又分两种情况:
(1)异步双工:接收和发送在同一个程序中,有两个不同的
子进程分别负责发送和接收
(2)异步单工:接收和发送是用两个不同的程序来完成。
2.同步
报文发送和接收是同步进行,既报文发送后等待接收返回报文。
同步方式一般需要考虑超时问题,即报文发上去后不能无限等
待,需要设定超时时间,超过该时间发送方不再等待读返回报
文,直接通知超时返回。

实际通信方式是这三类通信方式的组合。比如一般书上提供的
TCP/IP范例程序大都是同步短连接的SERVER/CLIENT程序。有的
组合是基本不用的,比较常用的有价值的组合是以下几种:

同步短连接Server/Client
同步长连接Server/Client
异步短连接Server/Client
异步长连接双工Server/Client
异步长连接单工Server/Client

其中异步长连接双工是最为复杂的一种通信方式,有时候经
常会出现在不同银行或不同城市之间的两套系统之间的通信。
比如金卡工程。由于这几种通信方式比较固定,所以可以预
先编制这几种通信方式的模板程序。

二.报文格式
通信报文格式多样性更多,相应地就必须设计对应的读写报文的接
收和发送报文函数。

(一)阻塞与非阻塞方式 
1.非阻塞方式
读函数不停地进行读动作,如果没有报文接收到,等待一段时间后
超时返回,这种情况一般需要指定超时时间。
2.阻塞方式
如果没有报文接收到,则读函数一直处于等待状态,直到有报文到达。

(二)循环读写方式
1.一次直接读写报文
在一次接收或发送报文动作中一次性不加分别地全部读取或全部
发送报文字节。
2.不指定长度循环读写
这一般发生在短连接进程中,受网络路由等限制,一次较长的报
文可能在网络传输过程中被分解成了好几个包。一次读取可能不
能全部读完一次报文,这就需要循环读报文,直到读完为止。

3.带长度报文头循环读写
这种情况一般是在长连接进程中,由于在长连接中没有条件能够
判断循环读写什么时候结束,所以必须要加长度报文头。读函数
先是读取报文头的长度,再根据这个长度去读报文.实际情况中,
报头的码制格式还经常不一样,如果是非ASCII码的报文头,还必须
转换成ASCII,常见的报文头码制有:
(1)n个字节的ASCII码
(2)n个字节的BCD码
(3)n个字节的网络整型码

ghmm.org

GHMM: General Hidden Markov Model library

The General Hidden Markov Model library (GHMM) is a freely available C library implementing efficient data structures and algorithms for basic and extended HMMs with discrete and continous emissions. It comes with Python wrappers which provide a much nicer interface and added functionality. The GHMM is licensed under the LGPL.

Defining a HMM with two states which output either heads or tails (think of two coins which get exchanged occasionally) is as easy as this:


> python
Python 2.6.4 (r264:75706, Mar 16 2010, 09:46:46) 
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ghmm
>>> sigma = ghmm.Alphabet(['h','t'])
>>> m = HMMFromMatrices(sigma, ghmm.DiscreteDistribution(sigma),
... [[0.9, 0.1], [0.3, 0.7]], [[0.5, 0.5], [0.2, 0.8]], [1.0, 0.0]) 
>>> print m
...
>>> help(m)
...
Here the last three arguments for HMMFromMatrices are the transition matrix, the emission matrix, and the initial distribution. For example 0.9 is the probability of staying in the first state. The first state uniformly emits heads or tails, the second state produces tails with a probability of 0.8. The HMM always starts in the first state.

Features

  • Discrete and continous emissions
  • Mixtures of PDFs for continous emissions
  • Non-homogenous Markov chains
  • Pair HMMs
  • Clustering and mixture modelling for HMMs
  • Graphical Editor HMMEd
  • Python bindings
  • XML-based file format
  • Portable (autoconf, automake)

Development

The GHMM is under active development by the Alexander Schliep's group for bioinformatics at Rutgers University. The development is hosted at Sourceforge http://sourceforge.net/projects/ghmm/, where you have access to the Subversion repository, mailing lists and forums.

Publications

The GHMM has been used in numerous scientific publications by the core group. A GHMM software publication is forthcoming. The GHMM has also extensively used as a teaching tool in Bioinformatics and machine learning classes at the Freie Universität Berlin and at Rutgers.

A. Sch鰊huth, I.G. Costa and A. Schliep. Semi-supervised Clustering of Yeast Gene Expression. In Cooperation in Classification and Data Analysis, Springer, 151–160, 2009.
Michael Seifert Analyzing Microarray Data Using Homogenous and Inhomogenous Hidden Markov Models. Diplomarbeit im Studiengang Bioinformatik, Martin-Luther-Universität Halle (2006)
A. Schliep, I. G. Costa, C. Steinhoff, A. A. Schönhuth. Analyzing Gene Expression Time-Courses IEEE/ACM Transactions on Computational Biology and Bioinformatics ( Special Issue on Machine Learning for Bioinformatics), 2005, 2(3):179-193.
Matthias Heinig Development of a Pair HMM based Gene Finder for the Paramecium Genome. Master Thesis, FU Berlin (2005)
I. G. Costa, A. Schönhuth, A. Schliep. The Graphical Query Language: a tool for analysis of gene expression time-courses , Bioinformatics, 2005, 21(10):2544-2545.
A. Schliep, C. Steinhoff, A. Sch鰊huth.Robust inference of groups in gene expression time-courses using mixtures of HMM. Proceedings of the ISMB 2004.
A. Schliep, B. Georgi, W. Rungsarityotin, I. G. Costa, A. Schönhuth The General Hidden Markov Model Library: Analyzing Systems with Unobservable States , Proceedings of the Heinz-Billing-Price 2004: 121-136,.
A. Schliep, A. Sch鰊huth, C. Steinhoff. Using Hidden Markov Models to Analyze Gene Expression Time Course Data. Proceedings of the ISMB 2003. Bioinformatics. 2003 Jul; 19 Suppl 1: I255-I263
B. Knab, A. Schliep, B. Steckemetz and B. Wichern. Model-Based Clustering With Hidden Markov Models and its Application to Financial Time-Series Data. In Between Data Science and Applied Data Analysis, Springer, 561–569, 2003.
B. Georgi. A Graph-based Apporach to Clustering of Profile Hidden Markov Models Bachelor Thesis, FU Berlin.
A. Weisse. Detecting Circular Permutations in Remote Homologue Proteins. Bachelor Thesis, FU Berlin.
Bernd Wichern. Hidden Markov Models for the analysis of data from saving and loan banks Ph.D. Thesis. ZAIK, University of Cologne, Germany (2001). In German.
Bernhard Knab. Extension of Hidden Markov Models for the analysis of financial time-series data Ph.D. Thesis. ZAIK, University of Cologne, Germany (2000). In German.

利用Socket HTTP协议获得HTML代码方法 - 空空空 - 博客园

利用Socket HTTP协议获得HTML代码方法


复制代码
        private string GetHTML(string strURL)
        {
            DateTime t1 
= DateTime.Now;
            Uri URI 
= new Uri(strURL);
            
string strHTML = "";//用来保存获得的HTML代码
            IPHostEntry gist = Dns.GetHostEntry(URI.Host);//获得当前URL的IP地址
            IPAddress ip = gist.AddressList[0];//提取IP地址
            IPEndPoint ipEnd = new IPEndPoint(ip, 80);//封装IP地址和端口
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//实例化Stock
            try { socket.Connect(ipEnd); }//自动循环捕捉连接
            catch { }
            
string sendstr = string.Format("GET {1} HTTP/1.1\r\nConnection:close\r\nHost:{0}\r\n\r\n", URI.Host, URI.PathAndQuery);//定义HTTP协议头部
            byte[] ms = UTF8Encoding.UTF8.GetBytes(sendstr);//将头部转换成byte形式
            socket.Send(ms);//发送
            int recv = -1;//定义接受数据长度
            byte[] data = new byte[1024];//用来保存接收数据
            do
            {
                recv 
= socket.Receive(data);
                strHTML 
+= Encoding.Default.GetString(data, 0, recv);
            } 
while (recv!= 0);
            DateTime t2 
= DateTime.Now;
            CeShiTime 
+="Socket:"+(t2 - t1).Milliseconds.ToString()+"  ";
            
return strHTML;
        }
复制代码
以上代码在VS2008环境测试通过

Scott Klement's web page


 
RPG IV Sockets Tutorial

View the tutorial on-line here

Or download it in one of these formats:

If you like, you can download all of the source code from the sockets tutorial as an OS/400 save file from this location:

http://www.scottklement.com/rpg/socktut/socktut.savf

I am always looking for input from readers on how I can improve my tutorial. If you have something to say, please use my Comment Form

解决google跳转问题 - - ITeye技术网站

Java代码  收藏代码
  1. FireFox浏览器:  
  2. 1.添加greasemonkey扩展,该扩展是允许浏览器使用UserScript,通过https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/添加  
  3. 2.安装Google SSL Privacy Real Link UserScript,通过http://userscripts.org/scripts/show/145014安装  
  4. 3.重启FireFox  
  5.   
  6. Google Chrome:  
  7. 1.下载Google SSL Privacy Real Link UserScript 扩展到本地,通过http://pan.baidu.com/share/link?shareid=104129&uk=973433708#dir/path=%2FOther%2FChrome%20Expansion下载最新版本  
  8. 2.打开谷歌浏览器,地址栏输入 chrome://chrome/extensions/  
  9. 3.把下载的扩展程序拖入谷歌浏览器任何位置即可  
  10. 4.重启Google Chrome  
  11.   
  12.   
  13. 注意:安装后请使用https://www.google.com.hk访问Google,点击搜索结果跳转会跳过Google的统计服务,直接加入目标地址  

Straight Google for Greasemonkey

Straight Google

关注我的人 新浪微博-随时随地分享身边的新鲜事儿

本就出众,何须隐瞒。
原文地址:https://www.cnblogs.com/lexus/p/2852190.html