简体   繁体   English

结合服务器和客户端python套接字

[英]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. 注意:我是套接字和网络的新手。

  1. First of all, you need to read some good book or some articles (google it - there are tons of them) about socket programming. 首先,您需要阅读一些有关套接字编程的好书或文章(在Google上可以读到很多)。 Stackoverflow is not enough. Stackoverflow不够。 You will just waste your time trying to learn something from random questions-answers. 您只会浪费时间尝试从随机问题中吸取一些知识。
  2. "Server" doesn't mean "receiving only mode" and "client" doesn't mean "sending only mode. There is full duplex connection between client and server. The difference is "server" will open a port and will wait for incoming connection. And "client" knowing server address will start this connection. Once the connection is established - "client" and "server" will be able to communicate full duplex. “服务器”不表示“仅接收模式”,“客户端”不表示“仅发送模式”。客户端和服务器之间具有全双工连接。区别在于“服务器”将打开端口并等待传入连接。知道服务器地址的“客户端”将启动此连接。建立连接后,“客户端”和“服务器”将能够进行全双工通信。
  3. Yes it is possible to "have both the server and client in the same application". 是的,可以“将服务器和客户端都放在同一个应用程序中”。 But it is rare behavior. 但这是罕见的行为。 You will need it on the server side if your server will need to for example make requests to some other servers (to google.com for example). 如果您的服务器需要例如向其他一些服务器(例如,向google.com)发出请求,则需要在服务器端使用它。 To do this you'll just need to handle both types of sockets: active and passive. 为此,您只需要处理两种类型的套接字:主动和被动。
  4. "how could I check if a server is active?". “如何检查服务器是否处于活动状态?”。 If the server is "ON" you will be able to connect to it. 如果服务器处于“开启”状态,则可以连接到该服务器。 If not - your attempt will return an error (an exception in Python). 如果不是,则您的尝试将返回错误(Python中的异常)。 That's it. 而已。
  5. There are two methods to use sockets: blocking mode and nonblocking mode. 有两种使用套接字的方法:阻塞模式和非阻塞模式。 In the latter case, your program does not need to wait for the end of the operation. 在后一种情况下,您的程序无需等待操作结束。 It can do their business at the same time of receiving or sending data through the socket. 它可以在通过套接字接收或发送数据的同时进行业务。
  6. I not recommend you use raw sockets. 我不建议您使用原始套接字。 It's not rational. 不合理 Raw sockets are needed only if you are trying to create some brand-new sockets framework with some unique features (but in order to do this you should be an expert in sockets development). 仅当您尝试创建具有某些独特功能的全新套接字框架时才需要原始套接字(但要做到这一点,您应该是套接字开发的专家)。 Just because there are already are tens of already existing frameworks only in Python (and some for a C/C++. libevent is one of the best of them). 仅仅因为Python中已经有几十个已经存在的框架(还有一些用于C / C ++的框架,所以libevent是其中最好的框架之一)。 One of Python async socket libraries is asyncio - the part of the Python standard library (for modern Python versions). Python异步套接字库之一是asyncio -Python标准库的一部分(适用于现代Python版本)。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM