TCP服务器/客户端:[Errno 32]管道损坏 [英] TCP Server/Client: [Errno 32] Broken pipe
问题描述
我正在尝试使用python为一个小游戏创建一个简单的多人游戏模式.我要做的是共享连接到服务器的每个播放器的位置.现在,尽管我一直在努力让甚至一个客户端与服务器通信,都使用套接字模块和json文件(尽管这似乎不会引起问题).
I am trying to create a simple multiplayer mode for a little game using python. What I want to do is sharing the position of each player connected to the server. Right now though I am stuck struggling with having even one client communicating with the server, using the socket module and json files (which don't seem to cause problems though).
我收到的错误以及出现的时间:
第二次尝试通过客户端发送邮件时,出现"[Errno 32]管道损坏"错误.根据一些谷歌搜索,这是在关闭连接时发生的.不幸的是,我看不到它的关闭位置.
As soon as I try to send something via client a second time I get the " [Errno 32] Broken pipe" error. According to some googling this happens when the connection is closed. Unfortunately I don't see where it is getting closed.
目前,我的代码几乎是这样的: http://thomasfischer .biz/python-simple-json-tcp-server-and-client/.自从我立即遇到这个问题以来,我并没有做太多改变.
Currently my code is pretty much this: http://thomasfischer.biz/python-simple-json-tcp-server-and-client/ . I didn't change much since I immediately ran into this problem.
服务器客户端:
import SocketServer
import json
class MyTCPServer(SocketServer.ThreadingTCPServer):
allow_reuse_address = True
class MyTCPServerHandler(SocketServer.StreamRequestHandler):
def handle(self):
try:
data = json.loads(self.request.recv(1024).strip())
print data
except Exception, e:
print "Exception wile receiving message: ", e
server = MyTCPServer(('127.0.0.1', 13373), MyTCPServerHandler)
server.serve_forever()
播放器客户端:
import SocketServer
import json
def __init__(self):
self.S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.S.connect(('127.0.0.1', 13373))
*lots of game related code*
pos = {'id':id, 'pos': (x, y, z)}
self.S.send(json.dumps(pos))
self.position = (x, y, z)
if __name__ == '__main__':
main() #to keep the game running and updating
错误:
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/__init__.py", line 123, in run
event_loop.run()
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/base.py", line 135, in run
self._run_estimated()
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/base.py", line 164, in _run_estimated
timeout = self.idle()
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/base.py", line 273, in idle
redraw_all = self.clock.call_scheduled_functions(dt)
File "/usr/local/lib/python2.7/dist-packages/pyglet/clock.py", line 309, in call_scheduled_functions
item.func(ts - item.last_ts, *item.args, **item.kwargs)
File "/home/tim/tools/Pyglet/PlayerClient.py", line 609, in update
self._update(dt / m)
File "/home/tim/tools/Pyglet/PlayerClient.py", line 641, in _update
self.S.send(json.dumps(pos))
socket.error: [Errno 32] Broken pipe
推荐答案
当连接的一端尝试发送数据而另一端已经关闭连接时,发生管道中断.
Broken Pipe occurs when one end of the connection tries sending data while the other end has already closed the connection.
self.S.send(json.dumps(pos))
尝试以上操作时,服务器已关闭连接.可能在*lots of game related code*
期间,客户端TCP意识到了这一点,但是客户端应用程序却没有.检查tcpdump b/w客户端和服务器.您应该从服务器上看到FIN或RST.
When the above is being attempted the server has already closed the connection. Possibly during the *lots of game related code*
And client side TCP is aware of that.But the client application is not. Check the tcpdump b/w the client and server. You should be seeing FIN or RST from the server.
您需要具有一种机制来捕获应用程序代码中的TCP事件(例如FIN/RST). TCP应用程序不应该包含捕获异步TCP事件的机制.
You need to have mechanism to capture TCP events like FIN/RST in your application code. TCP applications should not be written with out the mechanisms to capture asynchronous TCP events.
这篇关于TCP服务器/客户端:[Errno 32]管道损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!