gpt4 book ai didi

c - 了解套接字创建和 select() 系统调用之间的差距

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:17 24 4
gpt4 key购买 nike

我知道只要注册的套接字缓冲区中有数据,就会触发 select()

如果这两个语句之间有延迟会发生什么。

FD_SET(listener, &read_fds);              //    &
(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)

如果数据包在这两个语句之间到达,会发生什么情况?

//create socket and listen for packets       &
FD_SET(listener, &read_fds);

假设 recv() 在触发 select 后完成。

  • 如果数据包在 select() 调用之前到达会发生什么制成。?
  • FD_ISSET 是否仍然检测到已经存在的数据包
    套接字缓冲区,或者只有在新数据包到达时才会检测到它
    选择被触发?

示例代码:

// add the listener to the master set

FD_SET(listener, &master);

// keep track of the biggest file descriptor

fdmax = listener; // so far, it's this one

// main loop

for(;;) {
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(4);
}

// run through the existing connections looking for data to read
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!

最佳答案

Understanding gap between socket creation and select() system call

在您的问题中,套接字创建和 select() 之间没有差距。

I am aware that select() will be triggered whenever there is a data in the registered socket buffer.

读取事件也是如此,它适用于已连接套接字的套接字接收 缓冲区。当监听套接字上存在入站连接或套接字发送缓冲区中有空间用于发送事件时,它也会触发。

what will happen if there is a delay between these two statements.

FD_SET(listener, &read_fds);              //    &
(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)

没什么不好的。它们之间发生的任何事件仍将被发出信号。但是第一个语句不是套接字创建,这与您的标题相反。

what will happen if packet arrives between these two statements?

//create socket and listen for packets       &
FD_SET(listener, &read_fds);

套接字发送缓冲区从创建套接字的那一刻起就存在,因此数据将进入缓冲区,因此当 select() 运行时,它将看到并触发读取事件。

Assume that recv() is done once select is triggered. What will happen if a packet arrives before the select() call is made.?

套接字发送缓冲区从创建套接字的那一刻起就存在,因此数据将进入缓冲区,因此当 select() 运行时,它将看到并触发读取事件。

does FD_ISSET still detects the packet which is already in socket buffer

是的。

or it will be detected only if new packet arrives and select gets triggered?

它总是会被检测到。

关于c - 了解套接字创建和 select() 系统调用之间的差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com