在套接字服务器运行时运行单独的代码?
如何运行一个套接字服务器来接受传入连接并处理该部分代码,同时不让代码等待新连接陷入同一循环中?
我刚刚开始尝试学习。 TCP 处理程序有用吗?
我只需要一些关于这个主题的简单例子。我想要在服务器中拥有命令部分之类的东西。所以我可以在服务器运行时做某些事情。
编辑:我想做的事情:
1 - TCP server for multiple clients
2 - Respond to more than one at a time when needed
3 - Text input availability at all time, to be used for getting/setting info
4 - A simple way to get/save client address info. Currently using a list to save them.
How can I have a socket server running that accepts incoming connections and deals with that part of the code, while not having code waiting for new connections stuck in that same loop?
I am just starting trying to learn. Would a TCP Handler be useful?
I just need some simple examples on this topic. I'm wanting something like having a commands portion in the server. So i can do certain things while the server is running.
EDIT: What I'm trying to do:
1 - TCP server for multiple clients
2 - Respond to more than one at a time when needed
3 - Text input availability at all time, to be used for getting/setting info
4 - A simple way to get/save client address info. Currently using a list to save them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 线程 中运行套接字服务器。
You can run your socket server in a thread.
Python 在
asyncore
模块中内置了对异步套接字处理的支持 (http:// /docs.python.org/library/asyncore.html)。异步套接字处理意味着您必须在代码(主循环)内执行至少一次套接字处理循环迭代:
取自文档的示例:
每次套接字接受连接时,循环都会调用
handle_accept
。每次可以从套接字读取数据时,都会调用handle_read
,依此类推。您可以通过这种方式使用 TCP 和 UDP 套接字。
Python has builtin support of asynchronous socket handling in
asyncore
module (http://docs.python.org/library/asyncore.html).Asynchronous socket handling means that You have to execute at least one iteration of socket processing loop inside Your code (main loop):
Example taken from documentation:
Each time the socket accepts the connection
handle_accept
is called by the loop. Each time the data is available to read from sockethandle_read
is called and so on.You can use both TCP and UDP sockets in this manner.
我不太确定你在问什么,但通常在服务器端,你会调用socket()、bind()和listen()来设置套接字,然后循环调用accept()。此accept() 调用将阻塞,直到建立客户端连接。
对于简单的服务器,您可以处理客户端在循环内发出的任何请求。对于现实世界的服务器,您需要生成一些其他机制(例如,新的线程或进程,具体取决于语言/平台)来异步处理请求,以便原始循环可以在accept()调用上再次迭代并继续回到监听连接。
有关 Python 中的更多信息和示例,请参阅 Python 套接字文档:
http://docs.python.org/ howto/sockets.html
I'm not exactly sure what you are asking, but normally on the server side, you make socket(), bind() and listen() calls to setup the socket, and then loop around an accept() call. This accept() call blocks until a client connection is made.
For simple servers, you handle whatever request the client makes within the loop. For real-world servers, you need to spawn some other mechanism (e.g. a new thread or process, depending on the language/platform) to handle the request asynchronously, so that the original loop can iterate again on the accept() call and go back to listening for connections.
See the Python socket doc for more info and examples in Python:
http://docs.python.org/howto/sockets.html