Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:18:02+00:00 2026-06-13T18:18:02+00:00

I’m writing a multiplayer game server and client in Python, using the built-in SocketServer’s

  • 0

I’m writing a multiplayer game server and client in Python, using the built-in SocketServer’s TCPServer and ThreadingMixIn because it seems easier than manually managing the socket and threading modules. (I’d like to stick with the built-in modules for this.) It uses a protocol similar to HTTP for communication (GTP).

Requests involving only one client already work. If a client sends the request “GET /index.html GTP/0.2“, the server just has to respond “GTP/0.2 200 OK” to that client. But if a game is going on between clients A and B (as recorded in the server’s state) and client A sends the request “TURN <my turn info> GTP/0.2“, then after taking player A’s turn, how does the server notify both players A and B of the changes?

Here are the essentials of my code so far:

import SocketServer
import socket, threading # not yet used

class ThreadingGameServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    def __init__(self, server_address, RequestHandlerClass):
        SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)
        self.players = []
        self.games = []

class GameRequestHandler(SocketServer.BaseRequestHandler):
    def setup(self):
        self.request_data = None
        self.response_data = None

    def handle(self):
        while True:
            self.request_data = self.request.recv(4096)
            if not self.request_data:
                break # Client disconnected
            # delegate handling to do_GET, do_POST, etc
            do_method = 'do_' + self.request_data.split()[0]
            if not hasattr(self, do_method):
                self.request.sendall("GTP/0.2 501 Not Implemented\r\n\r\n")
                continue
            try:
                do = getattr(self, do_method)
                do()
            except Exception as e:
                break

    def do_GET(self):
        body = '<contents of {}>'.format(self.my_request.param)
        data = "GTP/0.2 200 OK\r\nContent-Length: {}\r\n\r\n{}".format(len(body), body)
        self.request.sendall(data)

    def do_LOGIN(self):
        """
        Create a player with the requested username and this handler.
        Add the player to self.server.players.
        Respond with 200 OK, body "Welcome!, <username>".
        """

    def do_PLAY(self):
        """
        If the requested opponent is not logged in and ready to play, respond with 403 Forbidden.
        Create a game with this player and the requested opponent.
        Remove the two players from self.server.players.
        Add the game to self.server.games.
        Respond with 200 OK, body "Begin game with <opponent>".
        How do I send "Begin game with <this player>" to the opponent as well?
        """

    def do_TURN(self):
        """
        If it is not this player's turn, respond with 403 Forbidden.
        Modify the game's state in self.server.games, including making it be the opponent's turn.
        Respond with 200 OK, body "<new game state>".
        How do I send the response to the opponent as well?
        """

    def do_EXIT(self):
        """
        If this player is logged in, log them out.
        If they are in a game, respond to their opponent with 200 OK, body "Game over" (how?).
        End their request handler.
        """

class GameClient(object):
    def __init__(self, server_address):
        self.socket = socket.create_connection(server_address)

    def run(self):
        """
        Read user input, e.g. "> login foobar".
        Send request to server, e.g. self.socket.sendall("LOGIN foobar GTP/0.2\r\n\r\n")
        Get server's reply via self.socket.recv(4096)
        Print the reply body, e.g. "Welcome, foobar!"
        """

# On the server machine
server = ThreadingGameServer((socket.gethostname(), 4242), GameRequestHandler)
server.serve_forever()

# On each client machine
client = GameClient(('server.mygame.com', 4242))
client.run()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:18:03+00:00Added an answer on June 13, 2026 at 6:18 pm

    There is probably a better way to do this, but the method I devised for myself ages ago (back when I was new with Python!) was to have a kind of inbox for each client. Each client then periodically asks the server for the contents if its inbox, and parses those commands accordingly.

    I’ll be keeping an eye on this question, I’d quite like to see any better methods than mine. I’d also recommend waiting for answers before trying this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.