[英]combining server and client python socket
I am attempting to make a local network chat program using python (which may later be in c), and TCP sockets. 我正在尝试使用python(以后可能在c中)和TCP套接字制作本地网络聊天程序。
My intent is to have the server listen on the current computer's address for incoming messages, and relay these to the client (how I am not sure yet). 我的目的是让服务器在当前计算机的地址上侦听传入的消息,并将其转发给客户端(我不确定如何)。 The client will be a simple gui which can send messages to active servers on local connections.
客户端将是一个简单的GUI,可以将消息发送到本地连接上的活动服务器。 In effect this setup will form something of a node network.
实际上,此设置将形成一个节点网络。
Is it possible to have both the server and client in the same application, preferably without using threads? 是否可以在同一应用程序中同时拥有服务器和客户端,最好不使用线程? I know the server will pause while listening for a connection, so the gui cannot respond.
我知道服务器在侦听连接时将暂停,因此gui无法响应。
If I cannot have both in the same application I was thinking of having 2 applications which communicate to each other, however threads seem like a better alterative as I'll be using 2 processes either way. 如果我不能在同一个应用程序中同时拥有这两个应用程序,我本想考虑使用2个相互通信的应用程序,但是线程似乎是更好的选择,因为我将以两种方式使用2个进程。 If I was to have separate apps how could I send strings to the client from server, to update the gui?
如果要使用单独的应用程序,如何从服务器向客户端发送字符串以更新GUI?
Can I make some sort of event (on the client side) which occurs when the wants server sends a message to client? 我可以在想要的服务器向客户端发送消息时进行某种事件(在客户端)吗?
In addition, how could I check if a server is active (available and bound to its computer address, awaiting a message) ? 另外,如何检查服务器是否处于活动状态(可用并绑定到其计算机地址,等待消息)?
Is my server/client relationship model reasonable? 我的服务器/客户端关系模型是否合理? Any suggestions?
有什么建议么?
I have checked similar socket questions on stack overflow, but haven't found any which specifically address my issue. 我已经检查了关于堆栈溢出的类似套接字问题,但是还没有找到专门解决我的问题的套接字问题。 I may have missed some, so please redirect my attention to them if they are found relevant.
我可能错过了一些内容,因此如果发现它们相关,请将我的注意力重新转向他们。
This question seems very similar: Python socket server: listening to multiple clients , however they do not seem to have a 'server' & client for each. 这个问题似乎非常相似: Python套接字服务器:侦听多个客户端 ,但是每个客户端似乎都没有一个“服务器”和客户端。
Note: I am new to sockets & networking. 注意:我是套接字和网络的新手。
libevent
is one of the best of them). libevent
是其中最好的框架之一)。 One of Python async socket libraries is asyncio
- the part of the Python standard library (for modern Python versions). asyncio
-Python标准库的一部分(适用于现代Python版本)。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.