python - Python 套接字模块错误:WinError 10057
问题描述
我正在尝试制作一个在线 Python 游戏,目前正在处理服务器文件和网络类(负责将服务器连接到客户端)。一切正常,但我一直在尝试将网络文件中的某些内容发送回服务器,但它不起作用。
我试着把它放在一个 try/except 循环中并让它打印错误。现在,控制台打印出这个。
None
[WinError 10057] 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
A fine day to you my friend!
None
Process finished with exit code 0
客户端文件:
import socket
IPV4 = socket.AF_INET
TCP = socket.SOCK_STREAM
SERVER = "192.168.1.77" # Replace with the ip address of the server
PORT = 5555
BITS = 2048
class Network:
def __init__(self):
self.client = socket.socket(IPV4, TCP)
self.server = SERVER
self.port = PORT
self.address = (SERVER, PORT)
self.id = self.connect()
print(self.id)
# So the idea is that when we decode the message on line 30 (rewrite this later), it will give us the string
# "Connected" to self.id, as it calls the function self.connect, which returns the message.
# self.id # This would be so that we could give an id to each player, and send specific things to each player
def connect(self):
try:
self.client.connect(self.address) # Connects our client to the server on the specified port
return self.client.recv(BITS).decode()
# Ideally when we connect we should send some form of validation token
except:
pass
def send(self, data):
try: # I think the problem is here!!!!
self.client.send(str.encode(data))
return self.client.recv(BITS).decode()
except socket.error as e:
print(e)
print("A fine day to you my friend!")
n = Network()
print(n.send("hello"))
# print(n.send("working"))
如果我没记错的话,问题就出在发送功能上。我收到的错误是我尝试编码和发送数据(self.client.send(str.encode(data))的结果。然后它给了我上面的错误消息。
服务器代码是:
import socket
from _thread import *
import sys
SERVER = "192.168.1.77" # (For now) the private ipv4 address of my computer (localhost)
PORT = 5555
MAX_PLAYERS = 2
BITS = 2048
IPV4 = socket.AF_INET
TCP = socket.SOCK_STREAM
# Setting up the socket
s = socket.socket(IPV4, TCP) # The arguments are the address family and socket type.
# AF_INET is the address family for Ipv4, and SOCK_STREAM is the socket type for TCP connections
try: # There is a chance that the port may be being used for something, or some other error may occur. If so, we want to find out what
s.bind((SERVER, PORT))
except socket.error as e: # This will
str(e)
s.listen(MAX_PLAYERS) # Opens up the port for connections
print("Waiting for a connection, Server Started")
def threaded_client(connection):
connection.send(str.encode("Connected")) # Sends an encrypted message to the client
reply = ""
while True:
try:
data = connection.recv(BITS)
reply = data.decode("utf-8") # Decodes the encrypted data
if not data: # If we try to get some info from the client and we don't, we're going to disconnect
print("Disconnected")
break # and break out of the try loop
else:
print("Received: {}".format(reply))
print("Sending: {}".format(reply))
connection.sendall(str.encode(reply)) # Sends our encrypted reply
except:
break
# Add possible errors when they occur
print("Lost connection")
connection.close()
while True:
connection, address = s.accept() # Accepts incoming connections and stores the connection and address
# Note: the connection is an object and the address is an ip address
print("Connected to {}".format(address))
start_new_thread(threaded_client, connection)
理想情况下,结果(假设我启动了服务器文件并且它正在运行而没有任何错误)如下:
Connected
hello
进一步解释......我得到“连接”的原因是因为在连接方法中我从服务器收到一条加密消息,我将其解码并返回到self.id。然后打印 self.id,表明它已连接到服务器。
解决方案
服务器也收到错误,这是问题的原因:
Waiting for a connection, Server Started
Connected to ('127.0.0.1', 1930)
Traceback (most recent call last):
File "C:\server.py", line 54, in <module>
start_new_thread(threaded_client, connection)
TypeError: 2nd arg must be a tuple
请改用以下内容:
start_new_thread(threaded_client, (connection,))
请注意,TCP 是一种流式协议,没有消息边界的概念,因此如果您没有在流中设计一个协议来确定消息的开始和结束位置,那么您最终会遇到一次发送多条消息的问题。
推荐阅读
- ruby-on-rails-6 - 当同时上传新文件时,Rails 6 ActiveStorage 标记为销毁的文件不会被销毁
- apache-kafka-connect - 安装 kafka 连接器时出现 ConfluentHubClient 异常
- android - 在绘制对象字体字段中使用自定义字体
- android - 如何将数据从 Intent 传递到 Fragment
- python - 删除数据框中某列中特定级别对应的行
- python - 如何在我的 pydroid 上将 .tar.gz 类型的文件放在 python 的路径中
- arduino - 为什么我的程序在写入此流后会重置?
- python - 根据用户所在国家/地区显示时间
- twilio - Twilio JS SDK 无法获取调用的状态变化
- javascript - Django:将 JavaScript 函数加载到 HTML 模板