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

Author Topic: Multiplayer Game Example  (Read 13793 times)

0 Members and 1 Guest are viewing this topic.

Ichinii

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Multiplayer Game Example
« on: March 31, 2013, 07:20:17 pm »
Hello Community :)

I really enjoy working with sfml and i allready coded some stuff. Now i want to start with networking and it's really hard. I got basic stuff working like sending data from the client to the server but I if i want to send data from the server to the client then the client needs open ports. I got some more problems and it's hard finding a solution without having examples.
It would be very nice if someone could give me a sourcecode of a programm or game which handles with data by multiple clients and sends back data with sfml library.

Thanks alot :)

Varonth

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Multiplayer Game Example
« Reply #1 on: March 31, 2013, 07:36:15 pm »
TcpSockets are bidirectional, and you just have to open port and forward ports to establish a connection. That's what the TcpListener is for, and that is just needed on the server side in most cases (unless you want a Peer-to-Peer topology).
Once a TcpSocket connects to a server, which is listening on a specific port and will accept connections, there will be some handshakes which will establish a connection on a ephemeral port.
So once connected you can send and receive data from a TcpSocket, and you just have to manage these TcpSocket objects, so that it sends data to the right client, and can distinguish which TcpSocket on the server is which client.

If you want to handle multiple clients with SFML you might want to take a look at the SocketSelector class, which also includes a nice example.

If you use UDP and want to use it bidirectional, then yeah you must also open ports on the client side.

Ichinii

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Multiplayer Game Example
« Reply #2 on: April 01, 2013, 12:30:27 am »
I have read that you should use udp sockets for games because they are way faster but what i don't get is how it's done in other games then. I mean are there ports which are allways opened which they use or do they use tcp sockets to send data to the client and udp to send data to the server?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Multiplayer Game Example
« Reply #3 on: April 01, 2013, 03:16:49 am »
There are some good points about networking in SFML's FAQ.

You should also first learn the principle of networking, before going to do a multiplayer game. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/