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

发布于 2024-08-14 12:47:10 字数 2398 浏览 3 评论 0原文

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

导入twisted.scripts.twistd 从twisted.protocols导入基本 来自twisted.internet导入协议,reactor fromtwisted.application import service, internet

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 文档中指定的另一种方法,数据已接收。添加此功能后,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 客户端能够使用此应用程序。

再次感谢。

I have the following code (almost an exact copy of the Chat server example listed here:

import twisted.scripts.twistd
from twisted.protocols import basic
from twisted.internet import protocol, reactor
from twisted.application import service, internet

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)

The server runs without error, and if I connect to it via Telnet, I can send data and the server prints to the console and relays it to all clients (as is expected). However, if I connect to it via a different tool (a MUD client), it never gets the data.

I have ensured that the client is sending the data (Traced the packets with Wireshark, and they're going across the wire), but the server either never receives it, or is choosing to ignore it for some reason.

I have tried this with two MUD clients, gmud, and JMC. If it is important, I am running Windows 7 x64.

Does anyone have any idea why this could be happening?

Thanks,

Mike

EDIT:

Thanks to the hints provided by Maiku Mori, I tried adding another method that was specified in the Twisted API Docs, dataReceived. Once this was added, the MUD clients worked perfectly, but Telnet is now sending every character as it's own set of data, instead of waiting for the user to press Enter.

Here's a snipped of the new code:

    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)

Has anyone experiences this before, and if so, how do you get around it? Ideally, I would like Telnet and MUD clients to work with this application.

Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2024-08-21 12:47:10

如果有人偶然发现这个问题并遇到类似的问题,我会将我的发现作为公认的答案,这样人们就不必像我一样寻找答案。

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

Hello, World

您的应用程序将收到它:

Hello, World\r

您可能需要在服务器端进行数据清理以保持秩序。我的最终代码如下:

导入twisted.scripts.twistd。
从twisted.protocols导入基本
来自twisted.internet导入协议,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)

感谢您的所有帮助。

In case anyone stumbles across this question with similar problems, I'm leaving my findings as the accepted answer so that people don't have to hunt the way I did.

I fixed the issue by changing the delimiter value from in my Twisted protocol from "\r\n" (default), to just "\n" (which is what my MUD clients send. This means that in Telnet, when you enter the string:

Hello, World

Your application will receive it as:

Hello, World\r

You may need to do data sanitation on the server side to keep things in order. My final code was as follows:

import twisted.scripts.twistd
from twisted.protocols import basic
from twisted.internet import protocol, reactor
from twisted.application import service, internet

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)

Thanks for all the help.

野稚 2024-08-21 12:47:10

您确定 MUD 客户端在每行之后发送行结束字符吗? lineReceived 仅在行结束字符发送后才会被调用。

编辑:

在这里我找到了 LineReceiver。您可以使用 dataReceived< /a> 方法来查看您是否确实获得了任何类型的数据。如果我记得你可以像 lineReceived 一样使用它。

Are you sure that the MUD clients send line ending chars after each line? The lineReceived will only be called after line ending char has been sent.

EDIT:

Here I found API docs for LineReceiver. You could play around with dataReceived method to see if you are actually getting any kind of data. If I recall you can use it just like lineReceived.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文