日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【转】setsocketopt getsocketopt比较全的参数说明

發(fā)布時(shí)間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】setsocketopt getsocketopt比较全的参数说明 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

setSocketOption各項(xiàng)的含義

? ? DELAY? ? ? ? 小緩沖寫入延遲值。如果為0,則禁用了TCP對(duì)于小緩沖區(qū)操作的Nagle算法。如果需要啟動(dòng)該算法則需要把該值設(shè)置為非0
?
??? KEEPLIVE??保持連接的特性。如果該值為0,則禁用了保持連接的特性。如果要啟動(dòng)該特性則要把該值設(shè)置為非0
??? LINGER? ? ? 關(guān)閉一個(gè)連接前等待未發(fā)送的數(shù)據(jù)發(fā)送完畢所經(jīng)過(guò)的秒數(shù)。如果該值為0,則禁用了該屬性
??? RCVBUF? ? 接受緩沖區(qū)的大小,單位字節(jié)
??? SNDBUF? ? 發(fā)送緩沖區(qū)的大小,單位字節(jié)

?

設(shè)置服務(wù)器端在3秒內(nèi)讀不到客戶的數(shù)據(jù)就超時(shí)
client.SetSocketOption(SocketOptionLevel.Socket,?SocketOptionName.ReceiveTimeout,3000);

?

TCP/IP中,發(fā)送端在發(fā)送消息時(shí),如若對(duì)方突然拔網(wǎng)線后,tcp的超時(shí)重發(fā)是9分鐘嗎?而這一點(diǎn)我們?cè)趕ocket編程中是沒(méi)法及時(shí)獲取send失敗的,它由tcp協(xié)議來(lái)控制重發(fā),重發(fā)一定次數(shù)超時(shí)后才通知我們sokcet有異常,整個(gè)過(guò)程是否這樣呢?我查了資料都沒(méi)講清楚。

1,server發(fā)送端發(fā)送消息時(shí), 突然對(duì)方拔掉網(wǎng)線。而發(fā)送端此時(shí)還認(rèn)為連接是ok,發(fā)送消息到緩沖隊(duì)列,然后由系統(tǒng)層tcp/ip把消息取走并有序的發(fā)送數(shù)據(jù),等待tcp段的ack確認(rèn)。
若接收端突然拔網(wǎng)線后,就沒(méi)有收到ack確認(rèn),就啟動(dòng)tcp重發(fā)機(jī)制和超時(shí)??是這樣嗎??
現(xiàn)在代碼里,希望能及時(shí)處理這種消息丟失情況,請(qǐng)問(wèn)朋友們?nèi)绾翁幚磔^好,謝謝。

2,我查了linux c接口,里面有send timeout選項(xiàng)。而java socket類沒(méi)有。
并且我懷疑c接口的send timeout選項(xiàng),可能是指把數(shù)據(jù)從緩存隊(duì)列拷到系統(tǒng)層的timeout時(shí)間??

我現(xiàn)在使用的是mina框架,項(xiàng)目是長(zhǎng)連接的IM系統(tǒng),我測(cè)試了拔網(wǎng)線的場(chǎng)景結(jié)果消息丟失,mina沒(méi)有拋異常。以上有錯(cuò)誤的認(rèn)識(shí),請(qǐng)朋友們指明。感激不盡。

? ? ?TCP/IP的超時(shí)與重傳使用的是“指數(shù)退避”的方式。分別為1、3、6、12、24、48和多個(gè)64秒。首次分組傳輸與復(fù)位信號(hào)傳輸之間的時(shí)間差約為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

總結(jié)

以上是生活随笔為你收集整理的【转】setsocketopt getsocketopt比较全的参数说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。