Python PodSixNet错误

时间:2015-06-18 14:38:46

标签: python python-2.7 pygame multiplayer

我正在尝试使用名为PodSixNet的多人游戏模块在我的某个游戏中使用,但在尝试运行客户端时,我在assyncore.py中收到错误。但是,服务器运行完美。我开发了简单的测试客户端和服务器程序,但是当我运行客户端时仍然会遇到相同的错误。这是我的客户端和服务器程序:

import PodSixNet, time
from PodSixNet.Connection import ConnectionListener, connection
from time import sleep

class MyNetworkListener(ConnectionListener):

    connection.Connect()


    def Network(self, data):
        print data


gui = MyPlayerListener()
while 1:
    connection.Pump()
    gui.Pump()

import PodSixNet, time
from time import sleep
from PodSixNet.Channel import Channel
from PodSixNet.Server import Server

class ClientChannel(Channel):

    def Network(self, data):
        print data

class MyServer(Server):

    channelClass = ClientChannel

    def Connected(self, channel, addr):
        print "new connection:", channel

myserver = MyServer()

while True:
    myserver.Pump()
    sleep(0.0001)

这是我在运行客户端时返回的错误:

    Traceback (most recent call last):
      File "C:\Users\Matt\Desktop\The 37th Battalion\PodSixNet Tests\client.py",  line 5, in <module>
        class MyNetworkListener(ConnectionListener):
      File "C:\Users\Matt\Desktop\The 37th Battalion\PodSixNet Tests\client.py",   line 7, in MyNetworkListener
    connection.Connect()
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)
  File "C:\Python27\lib\asyncore.py", line 418, in __getattr__
    retattr = getattr(self.socket, attr)

此错误一直持续到我达到最大递归深度。非常感谢帮助。

谢谢, 大卫

1 个答案:

答案 0 :(得分:2)

首先,你必须告诉服务器它应该监听哪个端口:

<强> server.py

import PodSixNet, time
from time import sleep
from PodSixNet.Channel import Channel
from PodSixNet.Server import Server

class ClientChannel(Channel):

    def Network(self, data):
        print data

class MyServer(Server):

    channelClass = ClientChannel

    def __init__(self, *args, **kwargs):
        Server.__init__(self, *args, **kwargs)

    def Connected(self, channel, addr):
        print "new connection:", channel

# use the localaddr keyword to tell the server to listen on port 1337
myserver = MyServer(localaddr=('localhost', 1337))

while True:
    myserver.Pump()
    sleep(0.0001)

然后,客户端必须连接到此端口:

<强> client.py

import PodSixNet, time
from PodSixNet.Connection import ConnectionListener, connection
from time import sleep

class MyNetworkListener(ConnectionListener):

    def __init__(self, host, port):
        self.Connect((host, port))

    def Network(self, data):
        print data

# tell the client which server to connect to
gui = MyNetworkListener('localhost', 1337)
while 1:
    connection.Pump()
    gui.Pump()

您不必在Connect()上呼叫connection,而是在ConnectionListener个实例上呼叫。

enter image description here