gpt4 book ai didi

python - 扭曲忽略从 MUD 客户端发送的数据?

转载 作者:可可西里 更新时间:2023-11-01 02:37:56 25 4
gpt4 key购买 nike

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

导入 twisted.scripts.twistd从 twisted.protocols 导入基本来自 twisted.internet 导入协议(protocol),reactor来自 twisted.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 客户端试过了,gmud , 和 JMC .如果重要的话,我运行的是 Windows 7 x64。

有谁知道为什么会发生这种情况?

谢谢,

迈克

编辑:

感谢Maiku Mori提供的提示,我尝试添加另一个在 Twisted API Docs 中指定的方法, 数据已收到。添加后,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 导入基本来自 twisted.internet 导入协议(protocol),reactor来自 twisted.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com