Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]

Author Topic: How to Python multiplayer?  (Read 5532 times)

wierd

  • Bay Watcher
  • I like to eat small children.
    • View Profile
Re: How to Python multiplayer?
« Reply #15 on: July 18, 2013, 03:22:29 pm »

The usual method is to have the host that acts as the "server" (game host, whatever) listen on a hard coded listen port, then negotiate a stateful connection on a new port.

Say, something like this:

Game host starts the game in host mode.
His/her client listens on some fixed port number. (Let's say 9000)
Another player wants to join the game.
They enter the IP address of the host in the "connect to" dialog.
Their client then connects, say, using local port 8000 to remote port 9000.
The host is actively listening for incoming connections, and catches the join request.
Using the "from" header in the IP datagram it received, it knows to send a port negotiation message, and to what address to send it to. Again, we use a hard coded port for the negotiation listen port. Port 9000.
The connecting client is sending requests out on 8000, and listening for responses on 9000.
It gets a response on 9000, telling it to switch to another port, and begin a stateful connection there.
The host can then resume listening to connection attempts on 9000.

That's how PASSIV mode works for old school FTP servers, so that many clients can connect to the server at once. FTP server listens on port 21, renegotiates a new port number for the user's activity, and frees 21 again for more incoming connections.

Remember that the sockets api let's you specify a local port to talk on, and a remote port to talk to. What IP address and port number used to send a datagram is stored in the IP datagram header, as the return address.

Logged

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: How to Python multiplayer?
« Reply #16 on: July 18, 2013, 03:24:35 pm »

This here seems exactly what you need. You just loop listen/accept and store the socket in a list or something.

The way I see it, if you've got a properly designed "game engine", playing singleplayer and multiplayer can be virtually the same thing as far as code goes. If your game reacts to event-based input, whether that event is generated by local keypresses or by a remote client sending a message (with a keypress!) is essentially irrelevant. And actually, implementing a remote event receiver should be relatively trivial.

Regarding security, just have all the game logic execute server side. Clients send commands, and if the state of the world changed, the server sends events notifying clients that it did so.

-----

Ninja'd while having dinner :)

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: How to Python multiplayer?
« Reply #17 on: July 18, 2013, 08:50:24 pm »

Learning how to link multiple players to one host would be nice.

Ideally, I'd like to see two example Python scripts for "Hello World" sent via socket, assuming that the IP of both computers would be entered manually.

Unfortunately, I rarely work in Python. The best I could do is C#, or C++ because those are the ones I generally use. It's pretty similar, you'd probably just have to change the names.

I wrote the C++ about 6 months ago, so its not very well written.
I stripped a lot out of the C# example, so it should be pretty simple. It could be simpler still, but its multi-threaded to allow the program to run other things while sending and receiving.

Spoiler: C++ (click to show/hide)

Spoiler: C# (click to show/hide)


Spoiler: C# Listen Functions (click to show/hide)

If you need any help, just ask.
« Last Edit: July 18, 2013, 08:55:02 pm by kytuzian »
Logged
Pages: 1 [2]