setSocketOption各项的含义
DELAY 小缓冲写入延迟值。如果为0,则禁用了TCP对于小缓冲区操作的Nagle算法。如果需要启动该算法则需要把该值设置为非0
KEEPLIVE 保持连接的特性。如果该值为0,则禁用了保持连接的特性。如果要启动该特性则要把该值设置为非0
LINGER 关闭一个连接前等待未发送的数据发送完毕所经过的秒数。如果该值为0,则禁用了该属性
RCVBUF 接受缓冲区的大小,单位字节
SNDBUF 发送缓冲区的大小,单位字节
设置服务器端在3秒内读不到客户的数据就超时
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout,3000);
TCP/IP中,发送端在发送消息时,如若对方突然拔网线后,tcp的超时重发是9分钟吗?而这一点我们在socket编程中是没法及时获取send失败的,它由tcp协议来控制重发,重发一定次数超时后才通知我们sokcet有异常,整个过程是否这样呢?我查了资料都没讲清楚。
1,server发送端发送消息时, 突然对方拔掉网线。而发送端此时还认为连接是ok,发送消息到缓冲队列,然后由系统层tcp/ip把消息取走并有序的发送数据,等待tcp段的ack确认。
若接收端突然拔网线后,就没有收到ack确认,就启动tcp重发机制和超时??是这样吗??
现在代码里,希望能及时处理这种消息丢失情况,请问朋友们如何处理较好,谢谢。
2,我查了linux c接口,里面有send timeout选项。而java socket类没有。
并且我怀疑c接口的send timeout选项,可能是指把数据从缓存队列拷到系统层的timeout时间??
我现在使用的是mina框架,项目是长连接的IM系统,我测试了拔网线的场景结果消息丢失,mina没有抛异常。以上有错误的认识,请朋友们指明。感激不尽。
TCP/IP的超时与重传使用的是“指数退避”的方式。分别为1、3、6、12、24、48和多个64秒。首次分组传输与复位信号传输之间的时间差约为9分钟。
These socket options can be set by using setsockopt(2) and read with getsockopt(2) with the socket level set to SOL_SOCKET for all sockets:
SO_ACCEPTCONN
Returns a value indicating whether or not this socket has been marked to accept connections with listen(). The value 0 indicates that this is not a listening socket, the value 1 indicates that this is a listening socket. Can only be read with getsockopt().
SO_BSDCOMPAT
Enable BSD bug-to-bug compatibility. This is used by the UDP protocol module in Linux 2.0 and 2.2. If enabled ICMP errors received for a UDP socket will not be passed to the user program. In later kernel versions, support for this option has been phased out: Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning (printk()) if a program uses this option. Linux 2.0 also enabled BSD bug-to-bug compatibility options (random header changing, skipping of the broadcast flag) for raw sockets with this option, but that was removed in Linux 2.2.
SO_BINDTODEVICE
Bind this socket to a particular device like "eth0", as specified in the passed interface name. If the name is an empty string or the option length is zero, the socket device binding is removed. The passed option is a variable-length null terminated interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an interface, only packets received from that particular interface are processed by the socket. Note that this only works for some socket types, particularly AF_INET sockets. It is not supported for packet sockets (use normal bind(8) there).
SO_BROADCAST
Set or get the broadcast flag. When enabled, datagram sockets receive packets sent to a broadcast address and they are allowed to send packets to a broadcast address. This option has no effect on stream-oriented sockets.
SO_DEBUG
Enable socket debugging. Only allowed for processes with the CAP_NET_ADMIN capability or an effective user ID of 0.
SO_ERROR
Get and clear the pending socket error. Only valid as a getsockopt(). Expects an integer.
SO_DONTROUTE
Don't send via a gateway, only send to directly connected hosts. The same effect can be achieved by setting the MSG_DONTROUTE flag on a socket send(2) operation. Expects an integer boolean flag.
SO_KEEPALIVE
Enable sending of keep-alive messages on connection-oriented sockets. Expects an integer boolean flag.
SO_LINGER
Sets or gets the SO_LINGER option. The argument is a linger structure.
struct linger {int l_onoff; /* linger active */int l_linger; /* how many seconds to linger for */ };
When enabled, a close(2) or shutdown(2) will not return until all queued messages for the socket have been successfully sent or the linger timeout has been reached. Otherwise, the call returns immediately and the closing is done in the background. When the socket is closed as part of exit(2), it always lingers in the background.
SO_OOBINLINE
If this option is enabled, out-of-band data is directly placed into the receive data stream. Otherwise out-of-band data is only passed when the MSG_OOB flag is set during receiving.
SO_PASSCRED
Enable or disable the receiving of the SCM_CREDENTIALS control message. For more information see unix(7).
SO_PEERCRED
Return the credentials of the foreign process connected to this socket. This is only possible for connected PF_UNIX stream sockets and PF_UNIX stream and datagram socket pairs created using socketpair(2); see unix(7). The returned credentials are those that were in effect at the time of the call to connect(2) or socketpair(2). Argument is a ucred structure. Only valid as a getsockopt().
SO_PRIORITY
Set the protocol-defined priority for all packets to be sent on this socket. Linux uses this value to order the networking queues: packets with a higher priority may be processed first depending on the selected device queueing discipline. For ip(7), this also sets the IP type-of-service (TOS) field for outgoing packets. Setting a priority outside the range 0 to 6 requires the CAP_NET_ADMIN capability.
SO_RCVLOWAT and SO_SNDLOWAT
Specify the minimum number of bytes in the buffer until the socket layer will pass the data to the protocol ( SO_SNDLOWAT) or the user on receiving ( SO_RCVLOWAT). These two values are initialised to 1. SO_SNDLOWAT is not changeable on Linux ( setsockopt fails with the error ENOPROTOOPT). SO_RCVLOWAT is changeable only since Linux 2.4. The select(2) and poll(2) system calls currently do not respect the SO_RCVLOWAT setting on Linux, and mark a socket readable when even a single byte of data is available. A subsequent read from the socket will block until SO_RCVLOWAT bytes are available.
SO_RCVTIMEO and SO_SNDTIMEO
Specify the receiving or sending timeouts until reporting an error. The parameter is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout has been reached then -1 is returned with errno set to EAGAIN or EWOULDBLOCK just as if the socket was specified to be nonblocking. If the timeout is set to zero (the default) then the operation will never timeout.
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(), and this doubled value is returned by getsockopt(). The default value is set by the rmem_default sysctl and the maximum allowed value is set by the rmem_max sysctl. The minimum (doubled) value for this option is 256.
SO_RCVBUFFORCE (since Linux 2.6.14 )
Using this socket option, a privileged ( CAP_NET_ADMIN) process can perform the same task as SO_RCVBUF, but the rmem_max limit can be overridden.
SO_REUSEADDR
Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For PF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. When the listening socket is bound to INADDR_ANY with a specific port then it is not possible to bind to this port for any local address.
SO_SNDBUF
Sets or gets the maximum socket send buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(), and this doubled value is returned by getsockopt(). The default value is set by the wmem_default sysctl and the maximum allowed value is set by the wmem_max sysctl. The minimum (doubled) value for this option is 2048.
SO_SNDBUFFORCE (since Linux 2.6.14 )
Using this socket option, a privileged ( CAP_NET_ADMIN) process can perform the same task as SO_SNDBUF, but the wmem_max limit can be overridden.
SO_TIMESTAMP
Enable or disable the receiving of the SO_TIMESTAMP control message. The timestamp control message is sent with level SOL_SOCKET and the cmsg_data field is a struct timeval indicating the reception time of the last packet passed to the user in this call. See cmsg(3) for details on control messages.
SO_TYPE
Gets the socket type as an integer (like SOCK_STREAM). Can only be read with getsockopt().
参考文章:
http://linux.die.net/man/7/socket