Python - Local Multiplayer System

Asked

Viewed 320 times

1

I’m trying to create a local multiplayer system, where will have a script for the "server" and several "clients" can connect. For now I will use in a simple test application, where users will create their accounts, log in and on the screen of each user who is connected will appear the list of everyone who is connected. The list would be changed in all clients when any user logged in or out.

The way I was doing, when the client wanted to do some action, like login for example, he would write a string in a text file, for example 'login user password'. Hence it had the server script that was in an infinite loop reading all these files (each open client would be 1, not necessarily the connected user) and writing the answer in the same file, for example:

Text file:

client_request:login usertest 1234
server_answer:wrong_password

Only I was slow, with some problems and I think I think I had been recording and reading text files non-stop. I wanted some idea of how I can do this in a better way, without using files !

If I haven’t made myself clear I try to explain again.

1 answer

1

The most common thing in this case is to use a socket connection - even with the client and the server being on the same machine. A great advantage you have is that the same program will work for clients and servers on the same machine, but also with clients on remote machines.

Well... to describe the idea of functioning and the whole implementation of sockets, and how you could do, would fill a book. : -) and indeed, there are good books about it. Simply explaining a connection using sockets does just what you want: the client program calls the server, and sends a series of data to it - and then gets a series of data back.

In practical terms, for exercise purposes - and not for a final program, you may want to use the Python "socket" module - in the language documentation you have a good tutorial: https://docs.python.org/2/howto/sockets.html

Of course, then you’ll fall into a problem that’s not 100% solved even with over 40 years of people working on it: how your program expects and listens to new customer connections and yet responds to customers who have already connected. Hence there are several possible approaches - one would be to use threads and one thread for each connection. Or multiple processes. Or micro-threads. Over the past few years, the most favored approach has been the use of asynchronous prooramation for this - if you are using Python 3.4, you can see the asyncio module - in other versions of Python, there is the external project "Trollius" that does this.

Link Úteis

Same question in the English OS

Tutorial Sockets

Browser other questions tagged

You are not signed in. Login or sign up in order to post.