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

Author Topic: Can't figure out how to send the message from server to all clients  (Read 3460 times)

0 Members and 2 Guests are viewing this topic.

silveeents

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hello, i recently started reading and studying about networking, and, looking through guides and tutorials, i tried to create a simple chat. I can only send from the client a message directed to the server, and the server will print it, but i can't figure out how the server can send it to all the clients.
Thank you in advance.

main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>

int main()
{
    std::vector<sf::TcpSocket*> clients;

    sf::TcpListener listener;
    listener.setBlocking(false);

    sf::SocketSelector selector;

    const std::string address = "127.0.0.1";
    const int port = 2100;

    // Select the side
    std::cout << "Choose which side you want: Client (c), Server (s)" << std::endl;
    char choice;
    std::cin >> choice;

    switch (choice) {
        case 's':
            listener.listen(port);
            selector.add(listener);

            while (true) {
                if (selector.wait()) {
                    if (selector.isReady(listener)) {
                        // Handle the listener update
                        sf::TcpSocket* incomingClient = new sf::TcpSocket;
                        if (listener.accept(*incomingClient) == sf::Socket::Done) {
                            // Handle socket selector for the incoming client
                            clients.push_back(incomingClient);
                            selector.add(*incomingClient);
                            std::cout << "Un client si è connesso" << std::endl;
                        } else delete incomingClient;
                    } else {
                        // Handle the socket update
                        char data[1024];
                        sf::Packet packet;

                        for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it) {
                            sf::TcpSocket& socket = **it;
                            if (selector.isReady(socket)) {
                                if (socket.receive(packet) == sf::Socket::Done) {
                                    // Extract the data
                                    packet >> data;

                                    std::cout << socket.getRemoteAddress() << ": " << data << std::endl;

                                    socket.send(packet);
                                }
                            }
                        }
                    }
                }
            }
        break;

        case 'c':
            sf::TcpSocket socket1;
            if (socket1.connect(address, port) == sf::Socket::Done) {
                std::cout << "Connected to the server" << std::endl;
            }

            char data[1024];
            sf::Packet packet;
            while (true) {
                std::cin >> data;

                packet << data;

                socket1.send(packet);

                packet.clear();

                char data1[1024];
                sf::Packet packet2;
                if (socket1.receive(packet2) == sf::Socket::Done) {
                    packet2 >> data1;

                    std::cout << "Server: "<< data1 << std::endl;
                }
            }
        break;
    }
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't figure out how to send the message from server to all clients
« Reply #1 on: November 09, 2017, 08:50:55 pm »
You iterate through all thr connected clients and send the message to them.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

silveeents

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can't figure out how to send the message from server to all clients
« Reply #2 on: November 09, 2017, 11:24:47 pm »
You iterate through all thr connected clients and send the message to them.

Already tried it like this, but it doesn't seem to work. I don't know what i'm doing wrong:
     
...
} else {                  
  // Handle the socket update
  char data[1024];
  sf::Packet packet;

  for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it) {
    sf::TcpSocket& socket = **it;
    if (selector.isReady(socket)) {
    if (socket.receive(packet) == sf::Socket::Done) {
    // Extract the data
    packet >> data;

    std::cout << socket.getRemoteAddress() << ": " << data << std::endl;

    socket.send(packet);

    for (std::vector<sf::TcpSocket*>::iterator it2 = clients.begin(); it2 != clients.end(); ++it2) {
      sf::TcpSocket& socket2 = **it2;

      if (it != it2) {
        char message[16] = "test";
        sf::Packet testPacket;
        testPacket << message;
        socket2.send(testPacket);
      }
  }
}
...
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't figure out how to send the message from server to all clients
« Reply #3 on: November 10, 2017, 02:05:38 am »
Well "it doesn't work" is not a problem description. ::)
When in doubt use a traffic capture like Wireshark to see what's going on.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/