0

我的朋友用 Python 制作了一个服务器,并希望我创建一个连接到它的 Android 应用程序,但我不知道从哪里开始。这是服务器的代码。任何人都可以帮助我或让我走上正确的道路吗?

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        a = data.split(':')
        print a
        if len(a) > 1:
            command = a[0]
            content = a[1]

            msg = ""
            if command == "iam":
                self.name = content
                msg = self.name + " has joined"

            elif command == "msg":
                msg = self.name + ": " + content

            print msg

            for c in self.factory.clients:
                c.message(msg)

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

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []

reactor.listenTCP(5633, factory)
print "Chat Server Started..."
reactor.run()
4

1 回答 1

0

如果您是网络编程的新手,那么阅读客户端-服务器模型套接字尤其是伯克利的套接字)是一个好的开始。

一旦你掌握了基本概念,你会想要查看Android 套接字文档,最后尝试一个教程

于 2013-01-28T00:31:54.473 回答