[Network] 获取 TCP queue 的数据的大小
2008-04-22 09:10:10| 分类:
net
| 标签:
|举报
|字号大中小 订阅
操作系统收到网络数据,回报存在对应的 mbuf 中,等待着进程去取这些数据。若我们仅想知道这些未读取的数据有多少,但并不真正的读取他们,可以通过下面的方法来获取。
int data_in_queue(int fd)
{
int n;
char buf[1024];
n = recv(fd, buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT);
if ( n == -1 ) throw("error");
return n;
}
由于系统不断接受数据,所以每次 data_in_queue() 获得到的值有可能是变化的。
------------
注意,不能用如下的方法:
recv(fd, NULL, 1024, MSG_PEEK | MSG_DONTWAIT); // FreeBSD 中 return errno = 14 (Bad Address)
char buf[16];
recv(fd, buf, 16, MSG_PEEK | MSG_DONTWAIT); // 如果 data in queue > 16 则也只返回 16
评论这张
转发至微博
转发至微博
评论