简体   繁体   English

在多个端口上运行套接字脚本

[英]Run Socket Script on Multiple Ports

What I want to do is run the following script on every port, 1025+. 我想做的是在1025+的每个端口上运行以下脚本。 What I am doing is making a Blackjack iPhone app that interacts with this script for online gaming. 我正在做的是制作与该脚本进行交互的在线二十一点iPhone应用程序。 The thing is, I would want to put this on each port manually by changing the port to listen each time for all the ports. 问题是,我想通过将端口更改为每次都监听所有端口来手动将其放在每个端口上。 How can I do it so that there is a new table on every port. 我该怎么做,以便每个端口上都有一个新表。 Each table has an ID the app will check for to see the amount of players and who is at the table. 每个桌子都有一个ID,应用会检查该ID,以查看玩家数量和桌子上的人。

The socket sets the ID for the Table class, but I need to be on multiple ports to be able to keep that table going and saving every player moves and such. 套接字设置了Table类的ID,但是我需要位于多个端口上才能保持该表的运行并保存每个玩家的动作等。

Bottom line, how can I make this script run on every port, how can I change the listening port by itself, and how can I have python make all Tables by itself on each port? 最重要的是,如何使此脚本在每个端口上运行,如何单独更改侦听端口,以及如何让python在每个端口上自行制作所有表?

class Table:
    def __init__(self, id):
        self.players = []
        self.positions = {'1': '', '2': '', '3': '', '4': ''}

    def sit(self, player_id, position):
        self.positions[position] = player_id

# --------------------------------------------- #
# --------------------------------------------- #


class Socket(Protocol):
    def connectionMade(self):
        #self.transport.write("""connected""")
        self.factory.clients.append(self)
        print "Clients are ", self.factory.clients

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

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



            msg = ""

        print msg

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

    def message(self, message):
        self.transport.write(message)


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

reactor.listenTCP(1025, factory)
print "Blackjack server started"
reactor.run()

Answering your question 回答你的问题

You ask: 你问:

  • How can I make this script run on every port? 如何使此脚本在每个端口上运行?
  • How can I change the listening port by itself? 如何单独更改监听端口?
  • How can I have python make all Tables by itself on each port? 如何让python在每个端口上单独制作所有表?

I think the answer here is to simply use a loop to bind the factory to as many ports as you want. 我认为这里的答案是简单地使用循环将工厂绑定到任意数量的端口。 However, since you're storing a list of clients in your factory as well, you'll need to create a new factory for reach port, as well. 但是,由于您还要在工厂中存储客户列表,因此也需要为到达端口创建一个新工厂。 So something like: 所以像这样:

factories = [ ]
for i in range(0, NUM_TABLES):
    factory = Factory()
    factory.protocol = Socket()
    factory.clicents = []
    factories.append(factory)
    reactor.listenTCP(1025 + i, factory)
    reactor.run()

You're using classes, so each factory keeps its own list of clients, each one has its own Socket instance to manage the connection. 您使用的是类,因此每个工厂都有自己的客户端列表,每个工厂都有自己的Socket实例来管理连接。 You don't show how Table instances are instantiated, but as long as each Socket or Factory instance instantiates and maintains a reference to the Table , this should allow you to have multiple connections, each with its own state. 您没有显示如何实例化Table实例,但是只要每个Socket或Factory实例都实例化并维护对Table的引用,就应该允许您具有多个连接,每个连接都有其自己的状态。

By keeping a list of all factories, you can iterate over them to make a list of running games, etc. 通过保留所有工厂的列表,您可以遍历它们以列出正在运行的游戏等。

Considering a different architecture 考虑不同的架构

While the above might work, it's not how client-server systems are typically architected. 尽管上面的方法可能有效,但通常不是客户-服务器系统的体系结构。 In particular, your system here requires your client to know what port to use. 特别是,这里的系统要求您的客户端知道要使用哪个端口。 That may work ad hoc when you're all in your living room together, but it's tedious and won't scale in general. 当您一起坐在起居室中时,这可能是临时工作的,但是这很繁琐,并且通常不会扩展。

What you want is something, like a webserver, that listens on one port to establish the connection, and then tells the client: "hey, your table id is 25, use that whenever you want to talk". 您想要的是某种东西,例如Web服务器,它在一个端口上侦听以建立连接,然后告诉客户端:“嘿,您的表ID为25,只要您想通话就使用它”。 In addition, this means making a list of tables available to the client, so they can pick one. 另外,这意味着向客户提供一个表列表,以便他们可以选择一个。 And, you can get fancier from there: a special expiring cookie given to client so that it doesn't accidentally hack/disturb a game that it's no longer part of, etc. 而且,您可以从那里得到更奇特的体验:为客户提供一个特殊的过期cookie,以使其不会意外地入侵/干扰不再属于其的游戏,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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