在处理其他数据时接收数据时套接字 select() 的预期行为?

发布于 2024-11-09 15:35:57 字数 1060 浏览 0 评论 0原文

我有一个关于套接字编程和在 while 循环中运行 select 命令的概念性问题。查看示例 echo 服务器的摘录(用 python 编写,但语言并不重要): http://ilab.cs.byu.edu/python/select/echoserver.html

while running: 
  inputready,outputready,exceptready = select.select(input,[],[]) 

  for s in inputready: 

    if s == server: 
        # handle the server socket 
        client, address = server.accept() 
        input.append(client) 

    elif s == sys.stdin: 
        # handle standard input 
        junk = sys.stdin.readline() 
        running = 0 

    else: 
        # handle all other sockets 
        data = s.recv(size) 
        if data: 
            s.send(data) 
        else: 
            s.close() 
            input.remove(s) 

问题:如果新客户端连接并且进程在 for 循环中执行代码时,预期的行为是什么那一刻发送数据?是否有某种缓冲区是默认套接字库的一部分,以便在后续的 select 调用中它会立即返回处理先前请求时传入的值?或者,当服务器实际上没有在 select() 中等待时,新的客户端连接是否会被忽略,因此总是有可能(尽管不太可能,因为我们正在谈论毫秒)请求被删除?

(注意:我对使用套接字的不同代码库进行了一些实证测试,似乎有一个缓冲区 - 如果这实际上是设计使然,那么该缓冲区的典型大小是多少?)

I have a conceptual question regarding socket programming and running the select command in a while loop. Check out this excerpt from a sample echo server (written in python, but the language doesn't matter): http://ilab.cs.byu.edu/python/select/echoserver.html

while running: 
  inputready,outputready,exceptready = select.select(input,[],[]) 

  for s in inputready: 

    if s == server: 
        # handle the server socket 
        client, address = server.accept() 
        input.append(client) 

    elif s == sys.stdin: 
        # handle standard input 
        junk = sys.stdin.readline() 
        running = 0 

    else: 
        # handle all other sockets 
        data = s.recv(size) 
        if data: 
            s.send(data) 
        else: 
            s.close() 
            input.remove(s) 

Question: what is the expected behavior while the process is executing code in the for loop if a new client connects and sends data at that moment? Is there a buffer of some kind that is part of the default sockets library such that on the subsequent select call it would immediately return with the value that had come in while it was processing the earlier request? Or would a new client connecting while the server is not actually waiting in the select() be ignored, and as such it is always possible (albeit unlikely, given we are talking milliseconds) for requests to be dropped?

(NOTE: I did some empirical testing on a different codebase that uses sockets and it appears that there is a buffer -- if that is in fact by design, what is the typical size of this buffer?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-11-16 15:35:57

内核网络堆栈异步处理与进程的新连接,并且永远不会丢弃传入连接,除非它超出 积压。

因此,除非您接收连接的速度太快以至于您的环路无法跟上它们,否则没有问题。 (也就是说,这是每秒传入连接的问题,而不是相对于循环的任何特定连接的时间。)

The kernel network stack handles new connections asynchronously to your process, and it will never drop an incoming connection unless it would exceed the listen backlog.

So unless you are receiving connections so fast that your loop is unable to keep up with them, there is no problem. (That is, it is a question of incoming connections per second, not the timing of any particular connection relative to your loop.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文