Android连接到Python服务器

时间:2013-01-27 23:31:52

标签: android python tcp

我的好友用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()

1 个答案:

答案 0 :(得分:0)

如果您不熟悉网络编程,那么一个良好的开端将会在client-server modelsocketsBerkeley's in particular上阅读。

掌握了基本概念后,您需要查看Android socket documentation,然后尝试a tutorial