简体   繁体   中英

Android connecting to a Python Server

My buddy made a server in Python and wanted me to create an Android app that connects to it, but I have no idea where to start with that. Here's the code to the server. Can anyone help me out or put me on the right path?

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()

If you're new to network programming, a good start will be reading up on the client-server model and sockets , Berkeley's in particular .

Once you have grasped the basic concepts, you'll want to check the Android socket documentation , and finally try a tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM