0

I am working on a small programming game/environment in Python to help my younger brother learn to code. It needs to operate over a network, and I am new to network programming. I am going to explain the concept of the game so that someone can point me in the best direction.

The idea is a simple grid of 25x25 'diodes,' squares with fixed positions and editable color values, essentially simulating a very small screen. In addition to the grid display, there is a command window, where Python code can be entered and sent to an instance of InteractiveConsole, and a chat window. A client needs to be able to send Python commands to the host, which will run the code, and then receive the output in the form of a string representing changes to the grid. My concept for doing this involves maintaining a queue on the host side of incoming and outgoing events to handle and relay to the clients on individual threads. Any given command/chat event will be sent to the host and relayed to all clients, including the client who created the event, so that those events are visible to all clients in their command/chat windows. All changes to the grid will originate with the host as a result of processing commands originated from clients and will also be sent out to all clients.

What I primarily don't understand is how to synchronize between all clients, i.e. how to know when a given item in the queue has been successfully sent out to all clients before clearing it from the queue, since any individual thread doing so prematurely will prevent the item from being sent to other clients. This is an extremely open-ended question because I understand that I will definitely need to consume some learning materials before I'm ready to implement this. I'm not asking for a specific solution but rather for some guidance on what general type of solution could work in my situation. I'm doing this in my spare time, so I don't want to spend a month going through networking tutorials that aren't pointing me in a direction that will be applicable to this project.

Rammschnev
  • 199
  • 1
  • 11

1 Answers1

1

My approach would be to use a udp server that can broadcast to multiple clients. So basically, all the clients would connect to this server during a game session, and the server would broadcast the game state to the clients as it is updated. Since your game is relatively simple this approach would give you real time updates.

Max Paymar
  • 588
  • 1
  • 7
  • 23
  • When you say 'broadcast,' is that substantially different from a regular `send()`/`recv()` relationship? – Rammschnev Apr 25 '17 at 17:38
  • Broadcast should only be used in situations where _every_ host on a LAN needs to get the message. Multicast is the proper thing to use in this type of situation. – Ron Maupin Apr 25 '17 at 17:45
  • @MaxPaymar @RonMaupin Okay, so I think I get the gist of things from the answers about multicasting on [this](http://stackoverflow.com/questions/603852/multicast-in-python#1794373) question. From what I gather, a UDP multicast socket is set up to transmit to all sockets listening on the given port, so one execution of `socket.sendto(...)` will send the same data to all listening sockets, without the need for threads from the looks of it. Does that seem right? – Rammschnev Apr 25 '17 at 18:17
  • Yep, every client listening to the multicast group socket will receive the data. – Max Paymar Apr 25 '17 at 18:20
  • @Rammschnev, host wanting to listen to multicasts subscribe to a multicast group. Multicast sources send a single message with the multicast group as the destination IP address, and every host subscribing to the multicast group will receive the message. – Ron Maupin Apr 25 '17 at 18:20