我有以下代码(几乎是here列出的聊天服务器示例的精确副本:

导入twisted.scripts.twistd
从twisted.protocols导入基本
来自twist.internet导入协议(protocol), react 堆
从twist.application导入服务,互联网

class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:
            c.message(line)

    def message(self, message):
        self.transport.write(message + '\n')

factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []

if __name__ == "__main__":
    print "Building reactor...."
    reactor.listenTCP(50000, factory)
    print "Running ractor...."
    reactor.run()
else:
    application = service.Application("chatserver")
    internet.TCPServer(50000, factory).setServiceParent(application)

服务器运行无误,如果我通过Telnet连接到服务器,则可以发送数据,服务器将打印到控制台并将其中继到所有客户端(这是预期的)。但是,如果我通过其他工具(MUD客户端)连接到它,它将永远无法获取数据。

我已经确保客户端正在发送数据(使用Wireshark跟踪了数据包,并且它们正在通过网络传输),但是服务器要么从不接收它,要么出于某种原因选择忽略它。

我已经使用两个MUD客户端gmudJMC进行了尝试。如果重要的话,我正在运行Windows 7 x64。

有谁知道为什么会这样?

谢谢,

麦克风

编辑:

感谢Maiku Mori提供的提示,我尝试添加在Twisted API Docs中指定的另一个方法dataReceived。添加之后,MUD客户端可以完美运行,但是Telnet现在发送每个字符作为其自己的数据集,而不是等待用户按Enter。

这是新代码的片段:
    def dataReceived(self, data):
        print "Dreceived", repr(data)
        for c in self.factory.clients:
            c.message(data)

#    def lineReceived(self, line):
#        print "received", repr(line)
#        for c in self.factory.clients:
#            c.message(line)

以前有没有人经历过,如果是这样,您如何解决呢?理想情况下,我希望Telnet MUD客户端可以与此应用程序一起使用。

再次感谢。

最佳答案

如果有人偶然遇到类似问题,我将我的发现保留为可以接受的答案,这样人们就不必像我一样追捕了。

我通过将Twisted协议(protocol)中的定界符值从“\r\n”(默认值)更改为仅“\n”(这是我的MUD客户端发送的值)来解决此问题。这意味着在Telnet中,当您输入字符串:

Hello, World

您的应用程序将收到以下内容:
Hello, World\r

您可能需要在服务器端进行数据清理,以使事情井井有条。我的最终代码如下:

导入twisted.scripts.twistd
从twisted.protocols导入基本
来自twist.internet导入协议(protocol), react 堆
从twist.application导入服务,互联网
class MyChat(basic.LineReceiver):
    def __init__(self):
        self.delimiter = "\n"

    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:
            c.message(line)

    def message(self, message):
        self.transport.write(message + '\n')

factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []

if __name__ == "__main__":
    print "Building reactor...."
    reactor.listenTCP(50000, factory)
    print "Running ractor...."
    reactor.run()
else:
    application = service.Application("chatserver")
    internet.TCPServer(50000, factory).setServiceParent(application)

感谢您的所有帮助。

关于python - 扭曲地忽略了MUD客户端发送的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1898411/

10-09 15:24
查看更多