Python - Local Multiplayer System

1

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

The way I was doing, when the client wanted to do some action, such as login for example, it recorded a string in a text file, for example 'login user password'. So I 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 saving the same response file, for example:

Text file:

client_request:login usertest 1234
server_answer:wrong_password

But it was slow, with some problems and I think it's a bit of a problem to keep recording and reading text files without stopping. I wanted some idea how I could do it in a better way, without using files!

If it was not clear, I try to explain it again.

    
asked by anonymous 09.07.2014 / 15:40

1 answer

1

The most usual case is to use a socket connection - even with the client and server being on the same machine. One 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 working and all the implementation of sockets, and how you could do it would be to fill a book. :-) and by the way, there are good books on that. 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 receives 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: link

Of course you will fall into a problem that is not 100% solved even with more than 40 years of people working on it: how your program expects and listens to connections from new customers and even 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 to use 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 "trollius" project that does that.

Useful links

Same question in SO English

Tutorial Sockets

    
10.07.2014 / 06:18