[英]How to change TCP Congestion Control algorithm using setsockopt() call from C++ code
Is it possible to change TCP congestion control algorithm from Cubic
to Reno
or vice versa using setsockopt call from C++ code in linux?是否可以使用来自linux 中的C++ 代码的setsockopt调用将TCP 拥塞控制算法从
Cubic
更改为Reno
,反之亦然?
I am looking for an example code of doing so.我正在寻找这样做的示例代码。
You can use the TCP_CONGESTION
socket option to get or set the congestion control algorithm for a socket to one of the values listed in /proc/sys/net/ipv4/tcp_allowed_congestion_control
or to any one of the values in /proc/sys/net/ipv4/tcp_available_congestion_control
if your process is privileged.您可以使用
TCP_CONGESTION
套接字选项来获取或插座的拥塞控制算法设置中列出的其中一个值/proc/sys/net/ipv4/tcp_allowed_congestion_control
或值中的任何一个/proc/sys/net/ipv4/tcp_available_congestion_control
如果您的进程有特权。
C example:示例:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char buf[256];
socklen_t len;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
len = sizeof(buf);
if (getsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, &len) != 0)
{
perror("getsockopt");
return -1;
}
printf("Current: %s\n", buf);
strcpy(buf, "reno");
len = strlen(buf);
if (setsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, len) != 0)
{
perror("setsockopt");
return -1;
}
len = sizeof(buf);
if (getsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, buf, &len) != 0)
{
perror("getsockopt");
return -1;
}
printf("New: %s\n", buf);
close(sock);
return 0;
}
For me outputs:对我来说输出:
Current: cubic
New: reno
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.