简体   繁体   English

Python 3.9 sockets 问题向客户端发送数据 sockets

[英]Python 3.9 sockets issue sending data to client sockets

I have been creating a chatroom application in python using socket and tkinter.我一直在使用套接字和 tkinter 在 python 中创建一个聊天室应用程序。 Whenever I try to send a message to the server I get the following error:每当我尝试向服务器发送消息时,都会收到以下错误:

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied不允许发送或接收数据的请求,因为未连接套接字并且(当使用 sendto 调用在数据报套接字上发送时)未提供地址

I'm not sure what could be causing this issue.我不确定是什么导致了这个问题。

Code:代码:

print("Sending message from server")

for clientKey in data.sockets.keys():
    data.sockets[clientKey]["socket"].send(bytes(f"{data.sockets['server']['username']}: {message}", "utf-8"))

log(f"{data.sockets['server']['username']}: {message}")
print("Sent message from server")

The program gets as far as the first print statement.程序到达第一个打印语句。

For context, I have a tkinter text widget and the function log(text) just inserts the text into the widget.对于上下文,我有一个 tkinter 文本小部件和 function log(text)只是将文本插入到小部件中。 It has nothing to do with the socket module.它与套接字模块无关。 For even more context, this is what a client's information looks like in the data class:对于更多上下文,这是data class 中客户信息的样子:

data.sockets["server"] = {"socket": sock,
    "username": "Host",
    "thread": threading.Thread(target = receiveMessages, daemon = True)}
data.sockets["server"]["thread"].start()

This is the server's information, but it is the same layout for the clients.这是服务器的信息,但客户端的布局相同。

The problem is that I never created the if clientKey != "server" , meaning every time I tried to send a message from the server, it would attempt to send a message to the server, which obviously isn't possible.问题是我从未创建过if clientKey != "server" ,这意味着每次我尝试从服务器发送消息时,它都会尝试向服务器发送消息,这显然是不可能的。

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

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