存档

‘网络’ 分类的存档

Linux服务器上用netstat查看的11种网络状态及长连接讨论

2009年11月1日 admin 1 条评论

说起来自己对网络这方面的知识掌握的很不好,回想读书的时候的计算机网络课也是补考通过的,最近有同事整理相关的资料,也借机分享给大家。

一、Linux服务器上11种网络连接状态:

1

图:TCP的状态机

通常情况下:一个正常的TCP连接,都会有三个阶段:1、TCP三次握手;2、数据传送;3、TCP四次挥手

注:以下说明最好能结合”图:TCP的状态机”来理解。

SYN:(同步序列编号,Synchronize Sequence Numbers)该标志仅在三次握手建立TCP连接时有效。表示一个新的TCP连接请求。

ACK:(确认编号,Acknowledgement Number)是对TCP请求的确认标志,同时提示对端系统已经成功接收所有数据。

FIN:(结束标志,FINish)用来结束一个TCP回话.但对应端口仍处于开放状态,准备接收后续数据。

1)LISTEN:首先服务端需要打开一个socket进行监听,状态为LISTEN. /* The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 */

2)SYN_SENT:客户端通过应用程序调用connect进行active open.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态置为SYN_SENT. /*The socket is actively attempting to establish a connection. 在发送连接请求后等待匹配的连接请求 */

3)SYN_RECV:服务端应发出ACK确认客户端的SYN,同时自己向客户端发送一个SYN. 之后状态置为SYN_RECV  /* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */

4)ESTABLISHED: 代表一个打开的连接,双方可以进行或已经在数据交互了。/* The socket has an established connection. 代表一个打开的连接,数据可以传送给用户 */

5)FIN_WAIT1:主动关闭(active close)端应用程序调用close,于是其TCP发出FIN请求主动关闭连接,之后进入FIN_WAIT1状态./* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求,或先前的连接中断请求的确认 */
6)CLOSE_WAIT:被动关闭(passive close)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT. /* The remote end has shut down, waiting for the socket to close. 等待从本地用户发来的连接中断请求 */

7)FIN_WAIT2:主动关闭端接到ACK后,就进入了FIN-WAIT-2 ./* Connection is closed, and the socket is waiting for a shutdown from the remote end. 从远程TCP等待连接中断请求 */

8) LAST_ACK:被动关闭端一段时间后,接收到文件结束符的应用程序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK . /* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */
9)TIME_WAIT:在主动关闭端接收到FIN后,TCP就发送ACK包,并进入TIME-WAIT状态。/* The socket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认 */

10)CLOSING: 比较少见./* Both sockets are shut down but we still don’t have all our data sent. 等待远程TCP对连接中断的确认 */

11)CLOSED: 被动关闭端在接受到ACK包后,就进入了closed的状态。连接结束./* The socket is not being used. 没有任何连接状态 */

TIME_WAIT状态的形成只发生在主动关闭连接的一方。

主动关闭方在接收到被动关闭方的FIN请求后,发送成功给对方一个ACK后,将自己的状态由FIN_WAIT2修改为TIME_WAIT,而必须再等2倍 的MSL(Maximum Segment Lifetime,MSL是一个数据报在internetwork中能存在的时间)时间之后双方才能把状态 都改为CLOSED以关闭连接。目前RHEL里保持TIME_WAIT状态的时间为60秒。

当然上述很多TCP状态在系统里都有对应的解释或设置,可见man tcp

二、关于长连接和短连接:

通俗点讲:短连接就是一次TCP请求得到结果后,连接马上结束.而长连接并不马上断开,而一直保持着,直到长连接TIMEOUT(具体程序都有相关参数说明).长连接可以避免不断的进行TCP三次握手和四次挥手.

长连接(keepalive)是需要靠双方不断的发送探测包来维持的,keepalive期间服务端和客户端的TCP连接状态是ESTABLISHED.目前http 1.1版本里默认都是keepalive(1.0版本默认是不keepalive的),ie6/7/8和firefox都默认用的是http 1.1版本了(如何查看当前浏览器用的是哪个版本,这里不再赘述)。Apache,java

一个应用至于到底是该使用短连接还是长连接,应该视具体情况而定。一般的应用应该使用长连接。

1Linux的相关keepalive参数

a、 tcp_keepalive_time – INTEGER
    How often TCP sends out keepalive messages when keepalive is enabled.
    Default: 2hours.

b、 tcp_keepalive_probes – INTEGER
    How many keepalive probes TCP sends out, until it decides that the
    connection is broken. Default value: 9.

c、 tcp_keepalive_intvl – INTEGER
    How frequently the probes are send out. Multiplied by
    tcp_keepalive_probes it is time to kill not responding connection,
    after probes started. Default value: 75sec i.e. connection
    will be aborted after ~11 minutes of retries.

2F5负载均衡上的相关参数说明

a、Keep Alive Interval

Specifies, when enabled, how frequently the system sends data over an idle TCP connection, to determine whether the connection is still valid.

Specify: Specifies the interval at which the system sends data over an idle connection, to determine whether the connection is still valid. The default is 1800 milliseconds.

b、Time Wait

Specifies the length of time that a TCP connection remains in the TIME-WAIT state before entering the CLOSED state.

  Specify: Specifies the number of milliseconds that a TCP connection can remain in the TIME-WAIT state. The default is 2000.

c、Idle Timeout

Specifies the length of time that a connection is idle (has no traffic) before the connection is eligible for deletion.

      Specify: Specifies a number of seconds that the TCP connection can remain idle before the system deletes it. The default is 300 seconds.

3、Apache的相关参数说明

以下是Apache/2.0.61版本的默认参数和说明

a、KeepAlive:

default On.Whether or not to allow persistent connections (more than

one request per connection). Set to "Off" to deactivate.

b、MaxKeepAliveRequests:

default 100.The maximum number of requests to allow

during a persistent connection. Set to 0 to allow an unlimited amount.

We recommend you leave this number high, for maximum performance.

c、KeepAliveTimeout:

default 15. Number of seconds to wait for the next request from the

same client on the same connection.

4、JAVA1.6的相关参数说明:

a、http.keepAlive=<boolean>
default: true

Indicates if keep alive (persistent) connections should be supported.

b、http.maxConnections=<int>
default: 5

Indicates the maximum number of connections per destination to be kept alive at any given time

分类: 网络 标签:

Java Mail 开发系列-协议介绍和简单示例

2009年9月23日 admin 没有评论

SMTP:简单邮件传输协议(Simple Mail Transfer Protocol,SMTP)由RFC821定义,它定义了发送邮件的机制,在JavaMail环境中,基于JavaMail的程序将和因特网服务供应商 ISP(internet Service Provider s)SMTP服务器通信.SMTP服务器会中转消息给接收方SMTP服务器以便最终让用户经由POP或者IMAP获得。

POP:代表邮局协议(Post Office Protocol).目前的版本是3.所以一般都称之为POP3.这个协议是由RFC1939定义的.POP是一种机制,因特网上多大数用户用它得到邮件.它规定每个用户一个邮箱的支持.使用POP协议的时候,用户的许多性能并不是由POP协议支持的,如查看几封新邮件消息这个功能,这些功能内建在如 Eudora或MicrosoftOutlook之类的程序中,它们记住一些事.所以在用JavaMail的时候,如果你想要这些信息,你就必须自己算了。

IMAP:是更高级的用户接收消息的协议,被定义在RFC2060中,IMAP 代表因特网消息访问协议(Internet Message Access Protocol),目前用的版本是4,所以也叫做IMAP4.在用到IMAP的时候,邮件服务器必须支持这个协议,不能仅仅把使用POP的程序用于 IMAP,并指望它支持IMAP所有性能。

MIME:是因特网邮件扩展标准(Multipurpose Internet Mail Extensions).它不是邮件传输协议,但是对于传输的内容的消息,附件以及其他的内容定义了格式.可以理解成一个定义合适的标准。

下面是一个最简单的发送电子邮件的示例

public class SimleJavaMailDemo {
    public  static void main(String[] args) throws MessagingException{
        Properties props = new Properties();
        String smtphost = "smtp.163.com";
        String user="";
        String password="";
        props.put("mail.smtp.host", smtphost);
        props.put("mail.smtp.auth","true");
        Session ssn = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(ssn);

        InternetAddress fromAddress = new InternetAddress("");
        message.setFrom(fromAddress);
        InternetAddress toAddress = new InternetAddress("");
        message.addRecipient(Message.RecipientType.TO, toAddress);

        message.setSubject("测试");
        message.setText("内容");

        Transport transport = ssn.getTransport("smtp");
        transport.connect(smtphost, user, password);
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        //transport.send(message);
        transport.close();
    }
}

Session类说明

Session 类定义了一个基本的邮件会话,所有的其他类都是由这个session才得意生效的,Session对象用java.util.Properties对象获取信息,如邮件服务器,用户名,密码及整个应用程序中共享的其他信息.类的构造器是此有的,private.它能用 getDefaultInstance()方法来共享.获取Session对象的方方法如下:

Properties props = new Properties();

Session session = Session.getDefaultInstance(props,null);

Null参数都是Authenticator对象,在这里没有使用.

对于大多数情况,共享的session已经足够用了.

Message类说明

Message 消息类,在获得了Session对象后,就可以继续创建要发送的消息.因为Message是个抽象类,您必须用一个子类,多数情况下为 java.mail.internet.MimeMessage.这个能理解成MIME类型和头的电子邮件消息.正如不同的RFC中定义的,虽然在某些头部域非ASCII字符也能被编译,但是Message头只能被限制用US-ASCII字符.要创建一个Message请将Session对象传递给 MimeMessage的构造器.

MimeMessage message = new MimeMessage(session);

一旦获得消息,就可以设置各个部分了.最基本的就是setContent()方法,例如/

message.setContent( “Hello ” , “ text/plain " );

如果知道在实用MimeMessage,而且消息是纯文本格式,就可以用setText()方法,它只需要代表实际内容的参数.(Mime类型缺省为text/plain)

用setSubject()方法设置subject(主题);

message.setSubject( “ 主题 ” );

InternetAddress类说明

InternetAddress类.若创建的地址只包含电子邮件地址,只要传递电子邮件地址给构造器就可以了.例如:

Address address = new InternetAddress( “ it5719@163.com ” );

若希望名字挨着电子邮件现实,就可以把它传递给构造器,如下:

Address address = new InternetAddress( “ it5719@163.com ” , ” 我心依旧 ” );

Authenticator类说明

Authenticator 与java.net类一样,JavaMailAPI也可以利用Authentcator通过用户名密码访问受保护的资源.对于JavaMail来说,这些资源就是邮件服务器,Authentcator类在javax.mail包中.要使用Authenticator,首先创建一个抽象的子类,并从

GetPasswordAuthentication 方法中返回passwordAuthentication实例,创建完成后,您必须向session注册Authenticator,然后在需要认证的时候会通知它,其实说白了就是把配置的用户名和密码返回给调用它的程序.例如:

Properties props = new properties();

Authenticator auth = new MailAuthenticator()//接口声明,创建自己新类的实例.

Session session = Session.getDefauItInstance(props,auth);

Transport类说明

Transport消息发送传输类,这个类用协议指定的语言发送消息,通常是SMTP,它是抽象类,它的工作方式与Session有些类似,尽调用静态方法send()方法,就OK了.例如:

Transport.send(message);

或者也可以从针对协议的会话中获取一个特定的实例,传递用户名和密码.发送消息,然后关闭连接,例如:

message.saveChanges();

transport transport = session.getTreansport( “ smtp ” );//指定的协议

transport.connect(host,username,password);

transport.sendMessage(message,message.getAllRecipients());

transport.close();

参考文档

http://www.blogjava.net/action/archive/2006/04/24/42794.html

分类: JAVA, 网络 标签: