java socket中有很多参数可以选择,这篇博客的目的是沉淀出这些参数的语义和用法,供自己以后查阅。
1、java socket参数选项总览
在JDK1.6中有如下参数选项:
1 public final static int TCP_NODELAY = 0x0001; 2 3 public final static int SO_BINDADDR = 0x000F; 4 5 public final static int SO_REUSEADDR = 0x04; 6 7 public final static int SO_BROADCAST = 0x0020; 8 9 public final static int IP_MULTICAST_IF = 0x10; 10 11 public final static int IP_MULTICAST_IF2 = 0x1f; 12 13 public final static int IP_MULTICAST_LOOP = 0x12; 14 15 public final static int IP_TOS = 0x3; 16 17 public final static int SO_LINGER = 0x0080; 18 19 public final static int SO_TIMEOUT = 0x1006; 20 21 public final static int SO_SNDBUF = 0x1001; 22 23 public final static int SO_RCVBUF = 0x1002; 24 25 public final static int SO_KEEPALIVE = 0x0008; 26 27 public final static int SO_OOBINLINE = 0x1003; 2、public final static int TCP_NODELAY = 0x0001; |
要理解这个参数,首先要理解Nagle算法,下面先说说这个Nagle算法
2.1 Nagle算法产生的背景
当网络传输中存在大量小包传输时,会严重影响传输效率。比如一个包,包头40字节,而真正的内容只有一个字节或者几个字节(典型的有Telnet),这样的传输效率是十分低下的。Nagle算法要解决的就是这种低效率的传输问题。
2.2 Nagle算法的原理
用通俗的话来说就是,把小包要发送的字节先缓存,当到达一定的阀值的时候再一次性传输。具体算法(伪代码)如下:
if there is new data to send
if the window size >= MSS and available data is >= MSS
send complete MSS segment now
else
if there is unconfirmed data still in the pipe
enqueue data in the buffer until an acknowledge is received
else
send data immediately
end if
end if
end if
其中MSS为maximum segment size的缩写,是TCP头部的一个字段,表示一个TCP段最大的数据承载量。