Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

c - Why changing value of SO_RCVBUF doesn't work?

I'm making a program which create a RAW socket in order to read all traffic. Between the call of socket() and recvfrom() (last one is in a loop to get out all packets from buffer) I wait 5s.

When I run the program, I send about 200 packets with hping3 command in ? faster mode ? (to fill in the buffer fastly) to my program. As soon as 5s are elapsed, my program extract about 150 packets from the buffer.

I try to change the size of the receive buffer to get better result:

int a = 65535;
if ( (setsockopt(sockfd, 0, SO_RCVBUF, &a ,sizeof(int)) ) < 0 )
{
    fprintf(stderr, "Error setting sock opts..
");
}

However, whatever is the value of ? a ?, 1 or 10000000, it seems nothing changes, I still get ~150 packets from the buffer.

What's the problem?

Edit: Value of ? a ? is verified with a getsockopt call.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may also be limited by the OS, if it still doesn't seem to be working. Check the values in:

/proc/sys/net/core/rmem_default
/proc/sys/net/core/rmem_max

If it's TCP as you say in your example, and not actually a raw socket, you can also check the values in:

/proc/sys/net/ipv4/tcp_mem

If you run cat on these files they'll show you the current settings. To change them permanently, use sysctl. It's a good idea to write these settings down before you start changing things. Here's a great tutorial on making those changes: http://fasterdata.es.net/fasterdata/host-tuning/linux/.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...