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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - silveeents

Pages: [1]
1
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);
      }
  }
}
...
 

2
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;
    }
}

Pages: [1]