setsockopt

What is the predictable behavior of changing SOL_SOCKET, SO_RCVBUF on the fly on a UDP socket?

爱⌒轻易说出口 提交于 2021-02-10 15:35:42
问题 What should be expected to happen if we resize the input buffer of a UDP server socket on the fly on a Linux system? setsockopt(sock, SOL_SOCKET, SO_RCVBUF, ...) I am particularly interested in these questions: If I shrink below what is currently in the buffer, would this simply drop the oldest/newest? datagrams properly, or could it flush everything that's there, or worse could it corrupt data such as truncating a datagram? Would shrinking the buffer even save memory or something prevents

Why no error when setting socket send/receive buffer size higher than sysctl max?

谁都会走 提交于 2021-02-08 10:21:04
问题 Why no error when setting socket send/receive buffer size higher than sysctl max (as I have demonstrated below)? is no error the "expected behavior"? My sysctl values for socket rmem_max and wmem_max are both set to 212992: net.core.rmem_default = 212992 net.core.rmem_max = 212992 net.core.wmem_default = 212992 net.core.wmem_max = 212992 net.ipv4.tcp_rmem = 4096 87380 6291456 net.ipv4.tcp_wmem = 4096 16384 4194304 net.ipv4.udp_rmem_min = 4096 net.ipv4.udp_wmem_min = 4096 vm.lowmem_reserve

setsockopt :SO_LINGER 选项设置

依然范特西╮ 提交于 2020-04-03 07:12:10
setsockopt 设置 SO_LINGER 选项 此选项指定函数close对面向连接的协议如何操作(如TCP)。内核缺省close操作是立即返回,如果有数据残留在套接口缓冲区中则系统将试着将这些数据发送给对方。 SO_LINGER选项用来改变此缺省设置。使用如下结构: struct linger { int l_onoff; /* 0 = off, nozero = on */ int l_linger; /* linger time */ }; 有下列三种情况: 1、设置 l_onoff为0,则该选项关闭,l_linger的值被忽略,等于内核缺省情况,close调用会立即返回给调用者,如果可能将会传输任何未发送的数据; 2、设置 l_onoff为非0,l_linger为0,则套接口关闭时TCP夭折连接,TCP将丢弃保留在套接口发送缓冲区中的任何数据并发送一个RST给对方,而不是通常的四分组终止序列,这避免了TIME_WAIT状态; 3、设置 l_onoff 为非0,l_linger为非0,当套接口关闭时内核将拖延一段时间(由l_linger决定)。如果套接口缓冲区中仍残留数据,进程将处于睡眠状态,直 到(a)所有数据发送完且被对方确认,之后进行正常的终止序列(描述字访问计数为0)或(b)延迟时间到。此种情况下,应用程序检查close的返回值是非常重要的

通过setsockopt设置SO_SNDBUF、SO_RCVBUF这连个默认缓冲区的值,再用getsockopt获取设置的值,发现返回值是设置值的两倍。

こ雲淡風輕ζ 提交于 2020-02-29 14:20:19
修改发送和接收的buffer: SEND_BUF_SIZE = 2501 RECV_BUF_SIZE = 2502 node1:/root/test#python t2.py Buffer size [Before]: 16384 Buffer size [Before]: 87380 alter---------------- 5002 5004 通过setsockopt设置SO_SNDBUF、SO_RCVBUF这连个默认缓冲区的值,再用getsockopt获取设置的值,发现返回值是设置值的两倍。 来源: CSDN 作者: zhaoyangjian724 链接: https://blog.csdn.net/zhaoyangjian724/article/details/104571693

How to set TCP_NODELAY flag when loading URL with urllib2?

半腔热情 提交于 2020-01-30 04:09:37
问题 I am using urllib2 for loading web-page, my code is: httpRequest = urllib2.Request("http:/www....com") pageContent = urllib2.urlopen(httpRequest) pageContent.readline() How can I get hold of the socket properties to set TCP_NODELAY ? In normal socket I would be using function: socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) 回答1: If you need to access to such low level property on the socket used, you'll have to overload some objects. First, you'll need to create a subclass of

setsockopt与getsockopt

自古美人都是妖i 提交于 2020-01-22 01:51:39
函数: # include <sys/types.h> # include <sys/socket.h> int getsockopt ( int sock , int level , int optname , void * optval , socklen_t * optlen ) ; int setsockopt ( int sock , int level , int optname , const void * optval , socklen_t optlen ) ; 参数释义: sock:网络文件描述符 level:选项所在协议层。 如果想要在套接字层面上进行配置,则将此项设置为SOL_SOCKET。 optname:需要访问的选项名 (后面会有)(取决于level) optval:对于getsockopt(),指向返回选项值的缓冲。对于setsockopt(),指向包含新选项值的缓冲。 optlen:对于getsockopt(),作为入口参数时,选项值的最大长度。作为出口参数时,选项值的实际长度。对于setsockopt(),现选项的长度。 函数功能: 获取或设置与某个套接字关联的选项。 level指定控制套接字的层次 . 可以取三种值 : 1 ) SOL_SOCKET : 通用套接字选项 . 2 ) IPPROTO_IP : IP选项 . 3 ) IPPROTO

How to set TCP keep alive interval for a specific socket fd (Not system wide) in VxWorks?

旧街凉风 提交于 2019-12-25 18:19:19
问题 I created a TCP socket in an application with KEEPALIVE enabled for this socket. I can see that keepalive packets are coming with the frequency of keepalive interval which i have already set in my system variable of iptcp, i.e 30secs. Socket Creation: if( ( sockFD = socket( AF_INET, SOCK_STREAM, 0 ) ) == ERROR ) { DS_SWX_ERROR( "socket(%d,%d,%d) failed.", AF_INET, SOCK_STREAM, 0 ); return; } VxWorks TCP System Variables: [vxWorks *]# sysvar list iptcp System variables: iptcp.KeepCnt=3 iptcp

CSocket中SetSockOpt()函数

拜拜、爱过 提交于 2019-12-25 00:33:04
简述: 设置套接口的选项。 #include <winsock.h> int PASCAL FAR setsockopt( SOCKET s, int level, int optname, const char FAR* optval, int optlen); s:标识一个套接口的描述字。 level:选项定义的层次;目前仅支持SOL_SOCKET和IPPROTO_TCP层次。 optname:需设置的选项。 optval:指针,指向存放选项值的缓冲区。 optlen:optval缓冲区的长度。 注释: setsockopt()函数用于任意类型、任意状态套接口的设置选项值。尽管在不同协议层上存在选项,但本函数仅定义了最高的“套接口”层次上的选项。选项影响套接口的操作,诸如加急数据是否在普通数据流中接收,广播数据是否可以从套接口发送等等。 有两种套接口的选项:一种是布尔型选项,允许或禁止一种特性;另一种是整形或结构选项。允许一个布尔型选项,则将optval指向非零整形数;禁止一个选项optval指向一个等于零的整形数。对于布尔型选项,optlen应等于sizeof(int);对其他选项,optval指向包含所需选项的整形数或结构,而optlen则为整形数或结构的长度。SO_LINGER选项用于控制下述情况的行动:套接口上有排队的待发送数据,且closesocket()调用已执行

getsockopt returns different IP_TOS value from the one set in setsockopt

岁酱吖の 提交于 2019-12-23 15:41:06
问题 I'm trying to use setsockopt to set IPTOS value to IPTOS_THROUGHPUT . The setsockopt call returned 0 . However the getsockopt shows the IP_TOS value is set to 1 , which is different from IPTOS_THROUGHPUT ( 0x8 ). Does anyone have idea what could have caused the mismatch of IPTOS value in the setsockopt and getsockopt ? Here's the log output: Set DSCP Marking on socket 26 setsockopt on socket 26 to 8 returns 0 DSCP marking on socket 26 is 1, different from expected 8 Below is the code: int

《关闭服务器,再次启动服务器提示bind:address already in use》

筅森魡賤 提交于 2019-12-14 15:58:05
  首先,我们先声明:bind:address already in use的存在是合理的,在服务端终止之后,会有一个TIME_WAIT的状态,再次打开会出现:bind:address already in use。   但是,当我们等上个2-4分钟后,然后在运行,就又没有这种情况了,很好解释,那只是因为在一定的时间内这个端口还被占用着,没有来的及释放,但是2-4分钟后,端口释放完毕,所以可以正常的运行这个程序了   服务器端可以尽可能的使用REUSEADDR(在绑定之前尽可能调用setsockopt来设置REUSEADDR)套接字选项,这样就可以使得不必等待TIME_WAIT状态就可以重启服务器了,也就是说:TIME_WAIT状态还是存在的,但是不影响我们重新启动服务器。   TIME_WAIT:我方主动调用close()断开连接,收到对方确认后状态变为TIME_WAIT。TCP协议规定TIME_WAIT状态会一直持续2MSL(即两倍的分 段最大生存期),以此来确保旧的连接状态不会对新连接产生影响。处于TIME_WAIT状态的连接占用的资源不会被内核释放,所以作为服务器,在可能的情 况下,尽量不要主动断开连接,以减少TIME_WAIT状态造成的资源浪费。目前有一种避免TIME_WAIT资源浪费的方法,就是关闭socket的LINGER选项。但这种做法是TCP协议不推荐使用的