Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Peer-to-peer example  (Read 3888 times)

0 Members and 1 Guest are viewing this topic.

daclable

  • Newbie
  • *
  • Posts: 1
    • View Profile
Peer-to-peer example
« on: July 05, 2014, 11:56:43 am »
Hi everybody,

I don't know much about networking and I've never been using SFML's Network module.
Luckily, there are lots of examples how to create basic application with network connection. However, all of them are Client<->Server based. I was not able to find any peer-to-peer example. I wonder if some of you guys could provide one.

// And one more thing - how could I test it on one PC? Should I just connect to the localhost (127.0.0.1)? Is it going to work?
« Last Edit: July 05, 2014, 02:33:33 pm by daclable »

nomdeplume

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Peer-to-peer example
« Reply #1 on: July 11, 2014, 10:50:32 am »
There isn't really much difference between "client <-> server" and "peer-to-peer" in terms of programming your network layer, other than that the "client <-> server" model allows you to make assumptions which simplify discovery and connection. In P2P, you just have one of the clients acting as the server for the other client, rather than a dedicated "server".

The main complication is that many clients on the internet are behind firewalls and/or NAT which prevents them from receiving direct connections. This is particularly applicable if your clients will be on the internet. A secondary complication is having a client on one PC discover the address of a client on another PC it can connect to.

For testing on a single PC, you'd really need to be able to run two copies of your program at the same time. Then you need them to connect to each other which is where you run into the main technical difference/hurdle: deciding which client will listen for the inbound connection, and of course, how the other client goes about finding its address.

Generally speaking, there's three approaches:
  • Make the user do it: one player selects "new server" and the other player selects "join server" and has to enter the IP address of the other player. Fairly easy to code, but assumes the players have the knowledge to do this.
  • Use local network broadcasts: this only works for LAN play. Basically the 'server' client will broadcast its existence to the local network, and any other clients on the same network can listen for those broadcasts.
  • Central metaserver/server browser: use a standard client-server model to have clients connect to a central server and provide information about themselves; other clients can then retrieve this list and use it to allow the player to select a 'server' (another client) to connect to.

You could also do a "mesh" P2P where all clients connect to each other, but that starts making things very complicated so it's rarely done in games. Usually you just have one client acting as a server for the other clients, while also showing the UI for its player.