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 に答える