我试图了解Socket-Send(Receive)-Buffer的概念 . 我写了这些代码:客户端:
int client = socket(AF_INET, SOCK_STREAM, 0);
int s = getsockopt(client, SOL_SOCKET, SO_SNDBUF, &sendBuffSize, &len);
int status = connect(client, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
printf("The send buff size is : %d.\n", sendBuffSize);
char buf[100000];
int n, wn;
int fd = open("./1.txt", O_RDONLY);
while ((n = read(fd, buf, sizeof(buf))) > 0) {
wn = write(client, buf, n);
printf("Write %d bytes.\n", wn);
}
服务器:我将连接的客户端设置为 Non-block ,并将此客户端添加到epoll中 . 一旦客户端将数据发送到服务器,我将主线程置于休眠状态[十秒] .
char buf[8192];
sleep(10);
int rn;
while ((rn = read(events[i].data.fd, buf, sizeof(buf))) > 0) {
printf("Read %d bytes.\n", rn);
}
客户端发送缓冲区大小为16384,服务器接收缓冲区大小为20000 [setsockopt] .
根据书:如果套接字发送缓冲区已满,客户端调用write函数将阻塞 .
但我得到了结果[客户]:Result
问题:
接收缓冲区大小发送缓冲区大小<100000;但是为什么写函数不会阻塞?
为什么服务器读取8192 6808 = 15000字节而不是连续读取8192字节?