请解释一下套接字缓冲区
我试图找到有关套接字编程的示例,并发现了这个脚本: http://stacklessexamples.googlecode.com/svn/trunk/examples/ networking/mud.py
当阅读这个脚本时,我发现了这一行: ListenSocket.listen(5)
据我了解 - 它从缓冲区读取 5 个字节,然后对其进行处理...
但是如果另一端发送超过 5 个字节会发生什么?
在该脚本的其他位置,它会根据 4 个命令检查输入并查看字符串中是否有 \r\n。 像“look”这样的命令加上 \r\n 不会占用超过 5 个字节吗?
艾伦
I was trying to find examples about socket programming and came upon this script:
http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py
When reading through this script i found this line:
listenSocket.listen(5)
As i understand it - it reads 5 bytes from the buffer and then does stuff with it...
but what happens if more than 5 bytes were sent by the other end?
in the other place of that script it checks input against 4 commands and sees if there is \r\n in the string. dont commands like "look" plus \r\n make up for more than 5 bytes?
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能会帮助您理解代码: http://www.amk.ca/python/howto /套接字/
This might help you understand the code: http://www.amk.ca/python/howto/sockets/
以下内容一般适用于套接字,但它应该有助于回答有关使用 Python 套接字的具体问题。
socket.listen() 在服务器套接字上使用来侦听传入的连接请求。
传递给listen的参数称为积压,它意味着套接字应该接受多少个连接并将其放入待处理的缓冲区中,直到完成对accept()的调用。 这适用于在调用listen() 和完成对accept() 的匹配调用之间等待连接到服务器套接字的连接。
因此,在您的示例中,您将待办事项设置为 5 个连接。
注意..如果您将待办事项设置为 5 个连接,则以下连接(第 6 个、第 7 个等)将被丢弃,并且连接套接字将收到一条连接错误消息(类似于“主机主动拒绝连接”消息)
The following is applicable to sockets in general, but it should help answer your specific question about using sockets from Python.
socket.listen() is used on a server socket to listen for incoming connection requests.
The parameter passed to listen is called the backlog and it means how many connections should the socket accept and put in a pending buffer until you finish your call to accept(). That applies to connections that are waiting to connect to your server socket between the time you have called listen() and the time you have finished a matching call to accept().
So, in your example you're setting the backlog to 5 connections.
Note.. if you set your backlog to 5 connections, the following connections (6th, 7th etc.) will be dropped and the connecting socket will receive an error connecting message (something like a "host actively refused the connection" message)
listenSocket.listen
的参数5
不是要读取或缓冲的字节数,而是backlog
:The argument
5
tolistenSocket.listen
isn't the number of bytes to read or buffer, it's thebacklog
: