eoferror:用完输入 - 插座,泡菜
因此,我目前正在尝试将插座与腌制一起使用,以简化数据传输。尽管我不断遇到以下错误。如果有帮助,我试图通过Internet发送信息而不是LAN。我还将在Pygame循环内部调用发送功能。
## This is the main module
def main():
run = True
n = Network()
p = n.getP()
clock = pygame.time.Clock()
while run:
p2 = n.send(p)
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
p.move()
redrawWindow(window, p, p2)
main()
## This is the network module
import socket
import pickle
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = ExternalIp
self.port = 5555
self.addr = (self.server, self.port)
self.p = self.connect()
def getP(self):
return self.p
def connect(self):
try:
self.client.connect(self.addr)
return pickle.loads(self.client.recv(2048))
except:
pass
def send(self, data):
try:
self.client.send(pickle.dumps(data))
return pickle.loads(self.client.recv(2048))
except socket.error as e:
print(e)
File "C:\Users\Nathan\Desktop\ALevelProject\Pre-Project\Server_Making\network.py", line 25, in send
return pickle.loads(self.client.recv(2048))
EOFError: Ran out of input
So I am currently trying to use sockets with pickling to have easy data transfer. Although I keep getting the below error. If it helps I am trying to send information across the internet not a LAN. I am also calling the send function inside of a pygame loop.
## This is the main module
def main():
run = True
n = Network()
p = n.getP()
clock = pygame.time.Clock()
while run:
p2 = n.send(p)
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
p.move()
redrawWindow(window, p, p2)
main()
## This is the network module
import socket
import pickle
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = ExternalIp
self.port = 5555
self.addr = (self.server, self.port)
self.p = self.connect()
def getP(self):
return self.p
def connect(self):
try:
self.client.connect(self.addr)
return pickle.loads(self.client.recv(2048))
except:
pass
def send(self, data):
try:
self.client.send(pickle.dumps(data))
return pickle.loads(self.client.recv(2048))
except socket.error as e:
print(e)
File "C:\Users\Nathan\Desktop\ALevelProject\Pre-Project\Server_Making\network.py", line 25, in send
return pickle.loads(self.client.recv(2048))
EOFError: Ran out of input
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
腌制使用实际有效载荷的其他字节来处理各种编码和填充目的,因此,2048字节数据(腌制)将更多。
您正在尝试解码固定的块大小,并且抱怨说应该有比它更多的数据。
我不确定开销是否已固定,用于给定数量和数据格式。如果是这样,您可以将其分解成大小,如果不是,则可能需要进行一些阅读多个较小的块,并尝试解析直到成功。
io
模块的 bufferedReader 可能对于后一种方法很有用。另一个选项是定义一个简单的长度预定格式,其中每个“消息”您发送以下泡菜块的大小,然后是腌制数据。
然后,读者可以做
chunk_len = some_decoder_func(buf.read(4)); pickle.loads(buf.read(chunk_len))
或thereabouts。附带说明,使用Pickle作为在游戏实例之间传输数据的一种方式可能不是一个好主意,请参阅
腌制
docs 以及在网上其他地方有关它可以利用的方式不需要对其他盲目信任的接收者进行不必要的事情。Pickling uses additional bytes to the actual payload to handle various encoding and marshaling purposes, so 2048 bytes data, when pickled, will be more.
You're trying to decode that fixed block size, and it's complaining that there should be more data than it has.
I'm not sure if the overhead is fixed, for a given quantity and format of data. If so you could factor that into your sizes, if not you might need to do some reading multiple smaller chunks and attempt parsing until it succeeds.
the
io
module's BufferedReader might be useful for the latter approach.Another option would be to define a simple length-prefixed format, where each "message" you send the size of the following pickle chunk, followed by the pickled data.
The reader can then do a
chunk_len = some_decoder_func(buf.read(4)); pickle.loads(buf.read(chunk_len))
or thereabouts.As a side note, using pickle as a way of transferring data between game instances might not be a great idea, see the warnings in e.g. the
pickle
docs and elsewhere online about the ways it can be exploited to do unwanted things to other recipients that blindly trust it.