Re: Writing a small battleship game server in Python

Discussion in 'Python' started by Michael Goettsche, Aug 12, 2005.

  1. On Thursday 11 August 2005 18:34, Dan wrote:
    > > The server should accept connections from new players and be able to
    > > handle multiple games concurrently.

    >
    > Multiple threads would be the way to go for a real application. But if
    > you want to avoid the complexity involved in threading and
    > synchronization in this exercize, you can avoid threads by using
    > "select" instead:
    > http://www.python.org/doc/2.3.5/lib/module-select.html
    >
    > The "select" function allows you to determine (without blocking) which
    > sockets have input available.
    >
    > Python's "select" is a wrapper for the C function with the same name. On
    > Unix you can learn more about it using "man select".
    >


    Thanks Dan.
    Assuming the server accepts connections in an endless loop, how would I handle
    communication without additional threads and create new games in that loop?
    Could you give me pseudo-code for this?

    Thanks,
    Michael.
    Michael Goettsche, Aug 12, 2005
    #1
    1. Advertising

  2. Michael Goettsche

    Mike Meyer Guest

    Michael Goettsche <> writes:

    > On Thursday 11 August 2005 18:34, Dan wrote:
    >> > The server should accept connections from new players and be able to
    >> > handle multiple games concurrently.

    >>
    >> Multiple threads would be the way to go for a real application. But if
    >> you want to avoid the complexity involved in threading and
    >> synchronization in this exercize, you can avoid threads by using
    >> "select" instead:
    >> http://www.python.org/doc/2.3.5/lib/module-select.html
    >>
    >> The "select" function allows you to determine (without blocking) which
    >> sockets have input available.
    >>
    >> Python's "select" is a wrapper for the C function with the same name. On
    >> Unix you can learn more about it using "man select".
    >>

    >
    > Thanks Dan.
    > Assuming the server accepts connections in an endless loop, how would I handle
    > communication without additional threads and create new games in that loop?
    > Could you give me pseudo-code for this?


    I recommended threads even more strongly than Dan did, so I'll answer
    as well.

    The pseudocode is:

    while 1:
    (rd, wr, xl) = select(rl, wl, xl)
    for sock in rd:
    socketdict(sock).do_read()
    for sock in wr:
    socketdict(sock).do_write()
    for sock in xl:
    socketdict(sock).do_except()

    Basically, you have objects that handle doing reads, writes, and
    "other things". You create a new object every time you get a new
    connection from your listennig socket, and store the object in
    socketdict indexed by the socket. You may have different types of
    objects in the dictionary, depending on what the socket is doing. In
    particular, the socket that is doing an accept() will have an object
    that creates new objects and stores them in the dictionary, whereas
    other sockets aren't likely to need that capability.

    When the objects socket has data ready to be read, it's do_read method
    will be called. When the socket is ready for a write, it's do_write
    method is called. When other conditions happen, it's do_except method
    is called.

    Those methods are pretty much the same whether you're using theads os
    asynchronious IO. Two critical differences: the I/O to the sockets must
    be done non-blocking, and you rally want to avoid operations that soak
    up lots of real time.

    There are frameworks around that already deal with all of this stuff
    for you. Twisted comes to mind, but I've never used it, and it may no
    be what I think it is.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Aug 13, 2005
    #2
    1. Advertising

  3. Michael Goettsche

    Paul Rubin Guest

    Michael Goettsche <> writes:
    > Assuming the server accepts connections in an endless loop, how
    > would I handle communication without additional threads and create
    > new games in that loop? Could you give me pseudo-code for this?


    I've always done this kind of thing with the SocketServer module
    and the threading mixin. You'd say something like:

    from SocketServer import SocketServer, ThreadingMixin

    class Battleship_server(ThreadingMixin, SocketServer):
    def handle(self, request):
    # this method gets called in a new thread when a new connection
    # arrives. Just handle the whole game for that connection here.

    You do need some synchronization to avoid concurrency hazards when
    the different threads touch shared data (like a global scoreboard)
    but it's not as bad as it sounds.
    Paul Rubin, Aug 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C-man
    Replies:
    2
    Views:
    963
    Martin
    Apr 5, 2004
  2. Michael Goettsche

    Writing a small battleship game server in Python

    Michael Goettsche, Aug 11, 2005, in forum: Python
    Replies:
    0
    Views:
    1,436
    Michael Goettsche
    Aug 11, 2005
  3. Dan
    Replies:
    3
    Views:
    891
    Michael Goettsche
    Aug 12, 2005
  4. KDawg44
    Replies:
    1
    Views:
    752
    Andrew Thompson
    May 24, 2007
  5. Shawn Milochik

    "Battleship" style game

    Shawn Milochik, Feb 25, 2009, in forum: Python
    Replies:
    13
    Views:
    646
    Falstaff
    Mar 26, 2009
Loading...

Share This Page